python可视化迷宫求解_用python求解迷宫

这篇博客介绍了一种使用Python解决迷宫问题的方法,通过递归算法探索可行路径,并进行可视化展示。在迷宫矩阵中,不同标记表示不同的地形,如起点(S),终点(E),已访问(.), 解决方案路径(o)等。通过调用`solve`函数,从起点开始,检查相邻的可通行瓷砖并递归地搜索解决方案。当找到出口时,路径会被标记为解决方案,并返回True,完成迷宫求解。" 113899333,10547977,JS实现日程安排插件:轻松排班与任务管理,"['js 排班插件', '前端开发', 'JavaScript', '网页交互']
摘要由CSDN通过智能技术生成

内联解释def solve(self, x=None, y=None):

# initializes x, y as start location

# occurs during first call of 'solve', i.e. zeroth recursion

if x==None:

x, y = self.start_x,self.start_y

# equivalent to, if the current (x, y) tile is walk-able

# implicitly ignores visited tiles

if self.maze[y][x] in (PATH,START):

# mark tile (x, y) as visited

self.maze[y][x] = VISITED

# if one of the adjacent tiles from (x, y) leads to a solution

# this is where the recursion occurs, i.e. 'solve' is called using the adjacent tiles

# this will be recursively called until one of the calls return True upon finding the exit

# when one of the calls encounter an exit, then this will create a chain reaction where each call returns True to its caller until the first call returns true

if self.solve(x+1,y) or self.solve(x-1,y) or self.solve(x,y+1) or self.solve(x,y-1):

# mark (x, y) as part of the solution

self.maze[y][x]=SOLUTION

# tells that a solution has been found, value used by caller

return True

# if the (x, y) tile is the exit then a solution has been found

elif self.maze[y][x] == EXIT:

return True

# if non of the if statements return, then by default no solution has been found where tile (x, y) is in the solution path.

return False

迷宫传说可漫游的磁贴

S开始平铺

E退出磁贴

.访问的磁贴

o是解决方案路径的一部分的平铺

所有其他瓷砖都是不可行走的瓷砖

迷宫前solve()

^{pr2}$

迷宫后solve()+-+-+-+-+-+-+-+-+-+-+

| |.........|...|...|

oo+-+-+-+-+.+.+-+.+.+

|ooooooo..|.|.....|.|

+-+-+-+o+-+.+-+-+-+.+

|ooo..|o|...|.......|

+o+o+.+o+.+-+.+-+-+-+

|o|o|.|o|.......|...|

+o+o+-+o+-+-+.+.+.+.+

|o|ooo|o|ooo|.|...|.|

+o+-+o+o+o+o+.+-+-+-+

|o|ooo|ooo|o|.......|

+o+o+-+-+-+o+.+-+-+.+

|o|ooooooooo|.|.....|

+o+-+-+ +-+-+.+-+-+-+

|o|ooo| |...| |

+o+o+o+-+-+-+-+.+ +-+

|o|o|o| |ooo....| |

+o+o+o+ +o+o+-+-+-+ +

|ooo|ooooo|oooooooooE

+-+-+-+-+-+-+-+-+-+-+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值