direct = [[-2,-1],[-1,-2],[1,-2],[2,-1],[2,1],[1,2],[-1,2],[-2,1]]
#走过的位置标记为1
def mark(maze,pos):
maze[pos[0]][pos[1]] = 1
print(pos)
#判断下一步位置的条件
def possible(maze,pos):
return (pos[0] in range(8)) and (pos[1] in range(8)) and (maze[pos[0]][pos[1]] == 0)
#结束条件
def end_path(maze):
for m in maze:
if 0 in m:
return False
return True
def find_path(maze,pos):
mark(maze,pos)
if end_path(maze):
print(pos,end = ' ')
return True
for i in direct:
next_pos = (pos[0] + i[0], pos[1] + i[1])
if possible(maze,next_pos):
if find_path(maze,next_pos):
print(pos,end = ' ')
return True
return False
#创建一个二维8*8列表,元素为0
def create_maze():
b = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]
return b
maze = create_maze()
find_path(maze,(0,0))