我知道已经发布了针对Euler 15的解决方案。从技术上讲,我已经找到了可行的解决方案(产生正确的数字),但是当它打印出路线时,这样做的方法是错误的
def legal_moves (row, column, grid):
legal_moves = []
if row != grid:
legal_moves.append ([row+1,column])
if column != grid:
legal_moves.append ([row,column+1])
if column == grid and row == grid:
return False
return legal_moves
def find_route (row,column,grid, route):
l_moves = legal_moves (row,column,grid)
if l_moves == False:
route.append ([row,column])
list_routes.append (route)
return
else:
route.append ([row,column])
if len(l_moves) == 1:
row = l_moves[0][0]
column = l_moves[0][1]
find_route (row,column,grid,route)
if len(l_moves) ==2:
row_a, column_a = l_moves[0][0], l_moves[0][1]
row_b, column_b = l_moves[1][0], l_moves[1][1]
find_route (row_a,column_a,grid,route)
find_route (row_b,column_b,grid,route)
grid = int(input("Enter A for grid size AxA: "))
list_routes = []
find_route(0,0,grid, route = [])
for item in list_routes:
print (item)
print ()
我将其用于2号电路板(因此输入:grid = 2)并打印了端子
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 2], [1, 2], [2, 2]]
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 2], [1, 2], [2, 2]]
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 2], [1, 2], [2, 2]]
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 2], [1, 2], [2, 2]]
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 2], [1, 2], [2, 2]]
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [2, 2], [1, 2], [2, 2], [0, 2], [1, 2], [2, 2]]
我无法弄清为什么第1行不仅仅打印:
[0, 0], [1, 0], [2, 0], [2, 1], [2, 2]
和第2行:
[0, 0], [1, 0], [1, 1], [2, 1], [2, 2]
等... 请有人可以帮助我了解原因吗? 谢谢