Python 查找两个选定单元格之间的最短路径(如果不能沿对角线移动)
我有一个矩阵:Python 查找两个选定单元格之间的最短路径(如果不能沿对角线移动),python,algorithm,Python,Algorithm,我有一个矩阵: maze = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1
maze = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]]
1-障碍
0-规则单元格
我想实现一个算法,在两个选定的单元格之间寻找最短路径(如果不能沿对角线走)。我尝试了A*算法,但没有给出正确的结果:
def astar(maze, start, end):
start_node = Node(None, start)
start_node.g = start_node.h = start_node.f = 0
end_node = Node(None, end)
end_node.g = end_node.h = end_node.f = 0
open_list = []
closed_list = []
open_list.append(start_node)
while len(open_list) > 0:
current_node = open_list[0]
current_index = 0
for index, item in enumerate(open_list):
if item.f < current_node.f:
current_node = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_node)
if current_node == end_node:
path = []
current = current_node
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1] # Return reversed path
children = []
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, 1), (1, -1)]:
node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) -1) or node_position[1] < 0:
continue
if maze[node_position[0]][node_position[1]] != 0:
continue
new_node = Node(current_node, node_position)
children.append(new_node)
for child in children:
for closed_child in closed_list:
if child == closed_child:
continue
child.g = current_node.g + 1
child.h = ((child.position[0] - end_node.position[0]) ** 2) + ((child.position[1] - end_node.position[1]) ** 2)
child.f = child.g + child.h
for open_node in open_list:
if child == open_node and child.g > open_node.g:
continue
open_list.append(child)
def astar(迷宫、开始、结束):
开始\节点=节点(无,开始)
开始节点。g=开始节点。h=开始节点。f=0
结束\节点=节点(无,结束)
end_node.g=end_node.h=end_node.f=0
打开列表=[]
已关闭的_列表=[]
打开\u列表。追加(开始\u节点)
而len(开放列表)>0:
当前\u节点=打开\u列表[0]
当前指数=0
对于索引,枚举中的项(打开列表):
如果.f项<当前节点.f:
当前节点=项目
当前索引=索引
打开\u列表.pop(当前\u索引)
关闭\u列表。追加(当前\u节点)
如果当前_节点==结束_节点:
路径=[]
当前=当前节点
虽然当前值不是“无”:
path.append(当前位置)
current=current.parent
返回路径[:-1]#返回反向路径
儿童=[]
对于[(0,-1),(0,1),(-1,0),(1,0),(-1,1),(1,-1)]中的新位置:
节点位置=(当前节点位置[0]+新位置[0],当前节点位置[1]+新位置[1])
如果节点位置[0]>(len(迷宫)-1)或节点位置[0]<0或节点位置[1]>(len(迷宫)[len(迷宫)-1])-1)或节点位置[1]<0:
持续
如果迷宫[节点位置[0]][节点位置[1]!=0:
持续
新节点=节点(当前节点,节点位置)
附加(新的_节点)
对于儿童中的儿童:
对于已关闭\u列表中的已关闭\u子项:
如果子项==关闭的子项:
持续
child.g=当前_节点.g+1
child.h=((child.position[0]-end_node.position[0])**2+((child.position[1]-end_node.position[1])**2)
child.f=child.g+child.h
对于“打开”列表中的“打开”节点:
如果child==open_node和child.g>open_node.g:
持续
打开\u列表。追加(子项)
请告诉我如何在Python语言中实现它,以使其正常工作。以下是如何在Python 3中实现BFS的示例:
import collections
def breadth_first_search(graph, root):
visited, queue = set(), collections.deque([root])
while queue:
vertex = queue.popleft()
for neighbour in graph[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
if __name__ == '__main__':
graph = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]]
breadth_first_search(graph, 0)
我希望这是能够帮助,请让我知道它如何进行 下面是一个如何在python 3中实现BFS的示例:
import collections
def breadth_first_search(graph, root):
visited, queue = set(), collections.deque([root])
while queue:
vertex = queue.popleft()
for neighbour in graph[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
if __name__ == '__main__':
graph = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]]
breadth_first_search(graph, 0)
我希望这是能够帮助,请让我知道它如何进行 以下是针对您的问题的BFS实现: 我们从感兴趣的起点开始排队,一旦到达终点就停止排队。在迭代的每一步中,我们的目标都是探索新的未探索状态,并将新点的距离设置为1+探索点的距离
从集合导入数据
图=[[0,0,0,0,1,0,0,0,0,0,0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]]
#向左、向右、上下移动
delta_x=[-1,1,0,0]
delta_y=[0,0,1,-1]
def有效(x,y):
如果x<0或x>=len(图)或y<0或y>=len(图[x]):
返回错误
返回(图[x][y]!=1)
def解算(开始、结束):
Q=deque([开始])
dist={start:0}
而len(Q):
curPoint=Q.popleft()
curDist=dist[电流点]
如果curPoint==结束:
回程凝乳器
对于zip中的dx、dy(δx、δy):
下一个点=(电流点[0]+dx,电流点[1]+dy)
如果无效(nextPoint[0],nextPoint[1])或dist.keys()中的nextPoint():
持续
dist[nextPoint]=curDist+1
Q.append(下一个点)
打印(求解((0,0)、(6,7)))
印刷品:#13
以下是针对您的问题的BFS实现: 我们从感兴趣的起点开始排队,一旦到达终点就停止排队。在迭代的每一步中,我们的目标都是探索新的未探索状态,并将新点的距离设置为1+探索点的距离
从集合导入数据
图=[[0,0,0,0,1,0,0,0,0,0,0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]]
#向左、向右、上下移动
delta_x=[-1,1,0,0]
delta_y=[0,0,1,-1]
def有效(x,y):
如果x<0或x>=len(图)或y<0或y>=len(图[x]):
返回错误
返回(图[x][y]!=1)
def解算(开始、结束):
Q=deque([开始])
dist={start:0}
而len(Q):
curPoint=Q.popleft()
curDist=dist[电流点]
如果curPoint==结束:
回程凝乳器
对于zip中的dx、dy(δx、δy):
下一个点=(电流点[0]+dx,电流点[1]+dy)
如果无效(nextPoint[0],nextPoint[1])或dist.keys()中的nextPoint():
持续
dist[nextPoint]=curDist+1
Q.append(下一个点)
打印(求解((0,0)、(6,7)))
印刷品:#13
我不确定A*是否有效,而你的实现是错误的,但这是BFS的一个明显案例,而不是一个*@juvian谢谢,如果记录是以矩阵的形式出现,你能展示一下它是如何工作的吗?你可以查看欢迎来到S