python - 找到两个选定单元格之间的最短路径(如果你不能走对角线)
问题描述
我有一个矩阵:
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)
请告诉我如何用 Python 语言实现它以使其正常工作。
解决方案
这是针对您的问题的 BFS 实现:https ://ideone.com/tuBu3G 我们以感兴趣的起点启动队列,并在访问终点后停止。在我们迭代的每一步,我们的目标是探索新的未探索状态,并将这个新点的距离设置为1 + 与探索点的距离。
from collections import deque
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]]
# To move left, right, up and down
delta_x = [-1, 1, 0, 0]
delta_y = [0, 0, 1, -1]
def valid(x, y):
if x < 0 or x >= len(graph) or y < 0 or y >= len(graph[x]):
return False
return (graph[x][y] != 1)
def solve(start, end):
Q = deque([start])
dist = {start: 0}
while len(Q):
curPoint = Q.popleft()
curDist = dist[curPoint]
if curPoint == end:
return curDist
for dx, dy in zip(delta_x, delta_y):
nextPoint = (curPoint[0] + dx, curPoint[1] + dy)
if not valid(nextPoint[0], nextPoint[1]) or nextPoint in dist.keys():
continue
dist[nextPoint] = curDist + 1
Q.append(nextPoint)
print(solve((0,0), (6,7)))
打印:# 13
推荐阅读
- python - 将类对象的返回值写入 csv 文件
- java - Android Java - 使用意图将值传递给第二个活动
- mockito - Mockito 独立运行但一起运行时失败
- github - 如何在没有合并提交或使用 CLI 的情况下使 GitHub 分叉保持最新?
- python - 流在 Flask + paramiko 中不起作用
- javascript - 无法让 vue-router 中的 cookie 工作。未定义的“这个”
- sql - 如何仅显示内部连接表列的最大值?
- laravel - 如何在axios中处理TokenMistmatchException
- php - MySQL:类似论坛的网站中的连接用户列表?
- csv - 在 Mule 中使用 Dataweave 动态合并两个 CSV 文件