📅  最后修改于: 2023-12-03 14:58:39.276000             🧑  作者: Mango
在阻塞网格中,每个点要么是空白的,要么是障碍物。给定起点和终点,求出连接这两个点的最短路径所需的最小圆形障碍物数目。
本算法使用BFS(宽度优先搜索)算法实现。
from collections import deque
# 定义网格中四个方向的移动坐标
DIRECTIONS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def bfs(start, end, grid):
queue = deque([(start, 0)])
visited = {start}
distance = {start: 0}
count = 0
while queue:
node, dist = queue.popleft()
if node == end:
return count
for dx, dy in DIRECTIONS:
x, y = node[0] + dx, node[1] + dy
neighbor = (x, y)
if neighbor in visited or not is_valid(neighbor, grid):
continue
visited.add(neighbor)
distance[neighbor] = dist + 1
if has_obstacle(node, neighbor, grid):
count += 1
queue.append((neighbor, dist + 1))
return -1
def is_valid(point, grid):
m, n = len(grid), len(grid[0])
x, y = point
if x < 0 or x >= m or y < 0 or y >= n:
return False
return grid[x][y] == 0
def has_obstacle(a, b, grid):
# 判断连接a和b的线段是否被障碍物阻挡
x1, y1 = a
x2, y2 = b
if x1 == x2:
# 连接的是同一行的两个点
for y in range(min(y1, y2), max(y1, y2) + 1):
if grid[x1][y] == 1:
return True
elif y1 == y2:
# 连接的是同一列的两个点
for x in range(min(x1, x2), max(x1, x2) + 1):
if grid[x][y1] == 1:
return True
else:
# 连接的是两个不在同一行或者列的点
x3, y3 = x1, y2
x4, y4 = x2, y1
if grid[x3][y3] == 1 and grid[x4][y4] == 1:
return True
return False
# 调用方法
start = (0, 0)
end = (3, 3)
grid = [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
result = bfs(start, end, grid)
print("最小圆形障碍物数目为:", result)