import matplotlib.pyplot as plt from queue import Queue s = (0, 0, 0) # (人所在位置, 距起点距离, 已经走的步数) t = (4, 4, -1) # 终点为平台(4,4) movable = [(1, 0), (0, 1), (-1, 0), (0, -1)] step = 6 lake = [[1,1,1,1,1,1], [1,0,0,0,0,1], [1,0,1,1,0,1], [1,0,0,1,0,1], [1,0,1,0,0,1], [1,1,1,1,1,1]] def bfs(start, end): queue = Queue() queue.put(start) visited = set() visited.add(start) while not queue.empty(): curr = queue.get() if curr == end: return curr for i, j in movable: x0, y0, z0 = curr x, y = x0 + i, y0 + j if 0 <= x < 6 and 0 <= y < 6 and lake[x][y] == 0: if z0 + 1 <= step: next_state = (x, y, z0 + 1) if next_state not in visited: visited.add(next_state) queue.put(next_state) parent[next_state] = curr return None def find_path(parent, end): path = [] while end: path.append(end) end = parent.get(end) path.reverse() return path parent = {} end_state = bfs(s, t) if end_state: path = find_path(parent, end_state) for x, y, _ in path: lake[x][y] = 2 fig, ax = plt.subplots(figsize=(8, 8)) for row in range(6): for col in range(6): if lake[row][col] == 0: ax.plot(row, col, '.', color='white', markersize=35) elif lake[row][col] == 1: ax.plot(row, col, '#', color='brown', markersize=35) elif lake[row][col] == 2: ax.plot(row, col, '*', color='blue', markersize=35) ax.plot(row, col, color='black', linestyle='-', linewidth=2) ax.set_xticks([]) ax.set_yticks([]) ax.set_xlim([-0.5, 5.5]) ax.set_ylim([-0.5, 5.5]) plt.show()修正这个代码
时间: 2023-11-14 21:07:02 浏览: 123
import numpy as np import matplotlib.pyplot as plt import math
在这段代码中,有一些问题。首先,你的导入语句和第一行代码应该分开写。其次,`parent`变量在`find_path`函数中未定义,需要传递给该函数。最后,你需要将`col`的值在循环内部更新,而不是在循环外部。下面是修正后的代码:
```
import matplotlib.pyplot as plt
from queue import Queue
s = (0, 0, 0) # (人所在位置, 距起点距离, 已经走的步数)
t = (4, 4, -1) # 终点为平台(4,4)
movable = [(1, 0), (0, 1), (-1, 0), (0, -1)]
step = 6
lake = [[1,1,1,1,1,1],
[1,0,0,0,0,1],
[1,0,1,1,0,1],
[1,0,0,1,0,1],
[1,0,1,0,0,1],
[1,1,1,1,1,1]]
def bfs(start, end):
queue = Queue()
queue.put(start)
visited = set()
visited.add(start)
parent = {}
while not queue.empty():
curr = queue.get()
if curr == end:
return curr
for i, j in movable:
x0, y0, z0 = curr
x, y = x0 + i, y0 + j
if 0 <= x < 6 and 0 <= y < 6 and lake[x][y] == 0:
if z0 + 1 <= step:
next_state = (x, y, z0 + 1)
if next_state not in visited:
visited.add(next_state)
queue.put(next_state)
parent[next_state] = curr
return None
def find_path(parent, end):
path = []
while end:
path.append(end)
end = parent.get(end)
path.reverse()
return path
parent = {}
end_state = bfs(s, t)
if end_state:
path = find_path(parent, end_state)
for row in range(6):
for col in range(6):
if lake[row][col] == 0:
plt.plot(row, col, '.', color='white', markersize=35)
elif lake[row][col] == 1:
plt.plot(row, col, '#', color='brown', markersize=35)
elif lake[row][col] == 2:
plt.plot(row, col, '*', color='blue', markersize=35)
plt.plot(row, col, color='black', linestyle='-', linewidth=2)
col = 0
plt.xticks([])
plt.yticks([])
plt.xlim([-0.5, 5.5])
plt.ylim([-0.5, 5.5])
plt.show()
```
这个修正后的代码将绘制一个可行路径的图形。
阅读全文