【问题标题】:Maze solving with python用python解决迷宫
【发布时间】:2014-05-07 03:57:51
【问题描述】:

我正在尝试制作一个迷宫求解器,它正在工作,除了路径被“o”标记,我希望它被标记为“>”、“

 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    

这是一个未解迷宫的例子:

####################################
#S#  ##  ######## # #      #     # #
# #   #             # #        #   #
#   # ##### ## ###### # #######  # #
### # ##    ##      # # #     #### #
#   #    #  #######   #   ###    #E#
####################################

这是使用上述代码解决的同一个迷宫版本:

####################################
#S#  ##  ######## # #oooooo#  ooo# #
#o#ooo#    oooo     #o#   ooooo#ooo#
#ooo#o#####o##o######o# #######  #o#
### #o##oooo##oooooo#o# #     ####o#
#   #oooo#  #######ooo#   ###    #E#
####################################

我想要得到的结果是:

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

这怎么可能?

【问题讨论】:

    标签: python recursion maze


    【解决方案1】:
    #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
    ...
    

    来自 Lix 示例。您需要取消注释 maze[y][x] = "o",您需要该行以防止节点被重新访问

    【讨论】:

      【解决方案2】:

      我建议不要立即将值设置为 'o' 并返回 'True',而是使用 if ... elif 设置字符然后返回。

      @marqs 指出我的原始答案将允许递归重新访问已被证明为错误的位置。因此,用 'o' 标记所有位置,以便它们不能被重新访问,然后循环遍历并将所有 'o' 重置为 ' '

      请注意,我建议 if ... elif,因为四个单独的 if 将始终检查其他可能性,即使它们已经通过找到真实路径被证明是错误的。

      # tag = ' '  Mistake on my part
      tag = 'o' # Mark so will not be revisited
      maze[y, x] = tag # Mark maze point as handled
      if self.solve(x+1,y) == True :  #right
          tag = '>'
      elif self.solve(x,y+1) == True :  #down
          tag = 'v'     
      elif self.solve(x-1,y) == True :  #left
          tag = '<'     
      elif self.solve(x,y-1) == True :  #up
          tag = '^'
      else:
        # All possible paths from here are false, back up and clear this point.
        tag = ' '
      # Note that if none of the tests were true, tag is left as ' '
      maze[y, x] = tag # Note C or C++ would use [x, y]
      return (tag != ' ')
      

      这将导致尝试的(错误)路径用“o”填充,然后在显示为不正确时返回空白。

      或者,您可以在其中留下一个“o”,找到真正的路径后,返回整个迷宫并清除标有“o”的点

      如果是这种情况,请删除 else: 并将返回更改为

      return (tag != 'o')
      

      【讨论】:

      • 它不起作用,我得到这个:RuntimeError: maximum recursion depth exceeded in cmp
      • @AboodMufti 你确定原来的情况没有得到递归错误吗?根据代码的工作方式,结果应该是相同的。
      • @AboodMufti 我很抱歉。我在我的代码中发现了逻辑错误并修复了它
      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2014-05-07
      • 2015-08-31
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多