#BFS
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 20 15:17:30 2018
@author: chuck
"""
from collections import deque
x = deque([]) #横坐标
y = deque([]) #纵坐标
s = deque([]) #代表步长
a = [[0 for col in range(8)] for row in range(8)] #初始化迷宫
b = [[0 for col in range(8)] for row in range(8)] #初始化标记数组
next_step = [[0,1], [1,0],[0,-1],[-1,0]] #迷宫的四种走法
start_x = 0 #迷宫的起点
start_y = 0
end_x = 7 #迷宫的终点
end_y = 7
map_array = [ [0,0,0,0,0,0,0,0],
[0,1,1,1,1,0,1,0],
[0,0,0,0,1,0,1,0],
[0,1,0,0,0,0,1,0],
[0,1,0,1,1,0,1,0],
[0,1,0,0,0,0,1,1],
[0,1,0,0,1,0,0,0],
[0,1,1,1,1,1,1,0] ]
for i in range(len(map_array)):
for j in range(len(map_array)):
a[i][j] = map_array[i][j] #将迷宫填入a中
head = 0 #队列的头和尾
tail = 0
x.appe
迷宫求解BFS/DFS-Python
最新推荐文章于 2024-08-24 11:25:20 发布
本文通过Python实现迷宫的BFS(广度优先搜索)和DFS(深度优先搜索)求解方法。首先使用BFS,利用deque数据结构存储横坐标、纵坐标和步长,通过不断扩展找到终点。接着,通过DFS,定义节点类并递归地探索所有可能的路径,直到到达终点。两种方法均能找出从起点到终点的路径。
摘要由CSDN通过智能技术生成