python迷宫问题模拟程序_用python解决迷宫

I am trying to make a maze solver, and it is working except that instead of the path being marked by "o" I want it to be marked with ">", "

def solve(self,x,y):

maze = self.maze

#Base case

if y > len(maze) or x > len(maze[y]):

return False

if maze[y][x] == "E":

return True

if maze[y][x] != " ":

return False

#marking

maze[y][x] = "o"

#recursive case

if self.solve(x+1,y) == True : #right

return True

if self.solve(x,y+1) == True : #down

return True

if self.solve(x-1,y) == True : #left

return True

if self.solve(x,y-1) == True : #up

return True

#Backtracking

maze[y][x] = " "

return False

This is an example of an unsolved maze:

####################################

#S# ## ######## # # # # #

# # # # # # #

# # ##### ## ###### # ####### # #

### # ## ## # # # #### #

# # # ####### # ### #E#

####################################

And this is the solved version of the same maze using the code above:

####################################

#S# ## ######## # #oooooo# ooo# #

#o#ooo# oooo #o# ooooo#ooo#

#ooo#o#####o##o######o# ####### #o#

### #o##oooo##oooooo#o# # ####o#

# #oooo# #######ooo# ### #E#

####################################

The result that I want to get to is:

####################################

#S# ## ######## # #>>>>>v# ^>v# #

#v#^>v# >>>v #^# >>>>^#>>v#

#>>^#v#####^##v######^# ####### #v#

### #v##^>>^##>>>>>v#^# # ####v#

# #>>>^# #######>>^# ### #E#

####################################

How is it possible to do this?

解决方案#marking

maze[y][x] = "o"

#recursive case

if self.solve(x+1,y) == True : #right

maze[y][x] = ">"

return True

if self.solve(x,y+1) == True : #down

maze[y][x] = "v"

return True

...

From Lix example. You need to uncomment maze[y][x] = "o", you need that row to prevent the node from being revisited

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值