import numpy as np
migong = '''
0 0 0 0 0 0 0 0 0 0
0 8 1 0 1 1 1 0 0 0
0 1 1 0 1 1 1 0 1 0
0 1 1 1 1 0 0 1 1 0
0 1 0 0 0 1 1 1 1 0
0 1 1 1 0 1 0 1 1 0
0 1 0 1 1 1 0 1 1 0
0 1 0 0 0 1 0 1 1 0
0 0 1 1 1 1 1 1 888 0
0 0 0 0 0 0 0 0 0 0'''
data = np.array(migong.split(), dtype = int).reshape((10,10))
def direction_set(data):
"""
函数功能,找到data中未被走过的地方,并同时记录该地方能够走的地方
"""
dir_set = {}
v, h = np.where(data > 0)
for i,j in zip(v, h):
key = str(i) + str(j)
if data[i, j+1] > 0:
dir_set[key] = [(i, j+1)]
if data[i+1, j] > 0:
if key in dir_set:
dir_set[key] += [(i+1, j)]
else:
dir_set[key] = [(i+1, j)]
if data[i, j-1] > 0:
if key in dir_set:
dir_set[key] += [(i, j-1)]
else:
dir_set[key] = [(i, j-1)]
if data[i-1, j] > 0:
if key in dir_set:
dir_set[key] += [(i-1, j)]
else:
dir_set[key] = [(i-1, j)]
return dir_set
step = []
key_old = '11'
print(data, '\n')
while True:
direct_set = direction_set(data)
if key_old == '88':
print(data)
print([i for i,j in step])
break
if key_old in direct_set:
step += [(key_old, direct_set[key_old])]
data[int(key_old[0]), int(key_old[1])] = -8
coors = direct_set[key_old][0]
key_old = str(coors[0]) + str(coors[1])
else:
for i in range(len(step)):
huish = step.pop()
key = huish[0]
values = huish[1]
if len(values) == 1:
data[int(key[0]), int(key[1])] = 1
else:
key_old = str(values[1][0]) + str(values[1][1])
step += [(key, values[1:])]
break
参考
class Maze(object):
def __init__(self, maze, start, end):
self.maze = maze
self.start = start
self.end = end
self.direction = [[-1, 0], [0, -1], [1, 0], [0, 1]]
def move(self, x, y):
"""
执行迷宫的上下左右操作
:param x: 起始坐标
:param y: 移动的方向
:return: 新的坐标
"""
return [x[0] + y[0], x[1] + y[1]]
def at(self, grid, x):
"""
传入一个maze和坐标,判断坐标是否越界,如果没有越界根据坐标返回对应maze中的数值
:param grid: maze
:param x: 查找坐标
:return: maze中的数值和状态
"""
if x[0] < 0 or x[0] >= len(grid):
return 0, False
if x[1] < 0 or x[1] >= len(grid[0]):
return 0, False
return grid[x[0]][x[1]], True
def walk(self):
"""
创建一个新的二维数组,self.maze每走一步,就在这个新的二维数组对应位置上记录行走的步数
:return: 记录了行走步数的二维数组
"""
steps = [[i * 0 for i in range(len(self.maze[0]))] for j in range(len(self.maze))]
Q = [self.start]
while len(Q) > 0:
index = Q[0]
if index == self.end:
break
Q = Q[1:]
for d in self.direction:
next = self.move(index, d)
val, ok = self.at(self.maze, next)
if not ok or val == 1:
continue
val, ok = self.at(steps, next)
if not ok or val != 0:
continue
if next == self.start:
continue
val, ok = self.at(steps, index)
if ok:
steps[next[0]][next[1]] = val + 1
Q.append(next)
return steps
def path(self, grid):
"""
根据steps计算最优路径
:param grid: steps迷宫图
:return: 存放路径的列表
"""
last = grid[len(maze) - 1][len(maze[0]) - 1]
lookup_path = [[len(maze) - 1, len(maze[0]) - 1], ]
while last > 0:
last -= 1
index = lookup_path[-1]
for d in self.direction:
next = self.move(index, d)
val, err = self.at(grid, next)
if val == last:
lookup_path.append(next)
break
return lookup_path
if __name__ == '__main__':
maze = [[0, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 1, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0]]
print("原始迷宫图:")
for k in maze:
for v in k:
print("%2d" % v, end=" ")
print("")
print("\n")
print("行走路线图:")
step = Maze(maze, [1, 0], [len(maze) - 2, len(maze[0]) - 1])
steps = step.walk()
for k in steps:
for v in k:
print("%2d" % v, end=" ")
print("")
print("\n")
print("走出迷宫共需要%s步\n" % steps[len(maze) - 2][len(maze[0]) - 1])
path = step.path(steps)
path.reverse()
print("最优路径是: ", path)
lookup_path = []
history_path = []
maze = [[0, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 1, 0], [1, 1, 1, 0, 0], [0, 1, 0, 0, 1], [0, 1, 0, 0, 0]]
for k in maze:
for v in k:
print(v, end=" ")
print("")
print("\n")
def up(location):
if location[0] == 0:
return False
else:
new_location = [location[0] - 1, location[1]]
if new_location in history_path:
return False
elif maze[new_location[0]][new_location[1]] == 1:
return False
else:
lookup_path.append(new_location)
history_path.append(new_location)
return True
def down(location):
if location[0] == len(maze) - 1:
return False
else:
new_location = [location[0] + 1, location[1]]
if new_location in history_path:
return False
elif maze[new_location[0]][new_location[1]] == 1:
return False
else:
history_path.append(new_location)
lookup_path.append(new_location)
return True
def left(location):
if location[1] == 0:
return False
else:
new_location = [location[0], location[1] - 1]
if new_location in history_path:
return False
elif maze[new_location[0]][new_location[1]] == 1:
return False
else:
history_path.append(new_location)
lookup_path.append(new_location)
return True
def right(location):
if location[1] == len(maze[0]) - 1:
return False
else:
new_location = [location[0], location[1] + 1]
if new_location in history_path:
return False
elif maze[new_location[0]][new_location[1]] == 1:
return False
else:
history_path.append(new_location)
lookup_path.append(new_location)
return True
start = [0, 0]
end = [5, 4]
print("start: %s --> end: %s\n" % (start, end))
lookup_path.append(start)
history_path.append(start)
while lookup_path[-1] != end:
now = lookup_path[-1]
if up(now) or down(now) or left(now) or right(now):
continue
lookup_path.pop()
print("final path: ", lookup_path)
参考2深度优先