Python 带障碍物的DFS路径搜索

Python 带障碍物的DFS路径搜索,python,artificial-intelligence,depth-first-search,Python,Artificial Intelligence,Depth First Search,我有下面的网格,我试图到达网格中的1,障碍物显示为2。机器人按以下顺序优先移动:上、右、下、左。起始位置旁边有一个*号 0 0* 0 0 2 0 1 1 0 到目前为止,我所做的是将网格的位置放到一个图表形式中,并按照优先级顺序从每个位置生成所有可能的移动。但我的问题是,当算法到达第二行的最后一部分时,它会陷入一个循环中。我正在尝试实现某种循环检测器,这样我就不会陷入那个循环中 我还应该提到,机器人可以访问相同的位置,只要它通过不同的路径。到目前为止,我得到的是:

我有下面的网格,我试图到达网格中的1,障碍物显示为2。机器人按以下顺序优先移动:上、右、下、左。起始位置旁边有一个*号

 0   0*  0
 0   2   0
 1   1   0
到目前为止,我所做的是将网格的位置放到一个图表形式中,并按照优先级顺序从每个位置生成所有可能的移动。但我的问题是,当算法到达第二行的最后一部分时,它会陷入一个循环中。我正在尝试实现某种循环检测器,这样我就不会陷入那个循环中

我还应该提到,机器人可以访问相同的位置,只要它通过不同的路径。到目前为止,我得到的是:

def dfs(grid,start):

    #find the routes in U,R,D,L order
    height = len(grid)

    width = len(grid[0])

    routes = {(i, j) : [] for j in range(width) for i in range(height)}

    for row, col in routes.keys():
      # Left moves
     if col > 0: 
          routes[(row, col)].append((row , col - 1))
      # Down moves    
     if row < height - 1:
          routes[(row, col)].append(((row + 1, col)))
     # Right moves
     if col < width - 1: 
         routes[(row, col)].append(((row , col + 1)))
     # Up moves
     if row > 0:
         routes[(row, col)].append(((row - 1, col)))

  #find the blocked ones

   blocked = {(i,j) for j in range(width) for i in range(height) if grid[i][j] == 2}

   path = []

   stack = [start]

   while stack:
     node = stack.pop()

     if node not in blocked:
         path.append(node)
         stack = []
         for x in routes[node]:
             stack.extend([x])

  return path
用手这样做表明路径应该如下:右、下、下、左、右、上、上、左、左、下、下

关于如何实现检测器的任何建议都将非常好,我对AI和python非常陌生,我整天都在努力解决这个问题……谢谢你尝试以下方法:

def dfs2(grid,pos,curr,height,width):
    x = pos[0]
    y = pos[1]    
    if grid[x][y]==1:
        print str(curr)
        return None
    elif grid[x][y] ==2:
        return None        
    last = curr[-1]    
    if x-1 >= 0 and last != 'DOWN':        
        curr.append('UP')
        dfs2(grid,(x-1,y),curr,height,width)
        curr.pop()
    if y+1 < width and last != 'LEFT':        
        curr.append('RIGHT')
        dfs2(grid,(x,y+1),curr,height,width)
        curr.pop()
    if x+1 < height and last != 'UP':
        curr.append('DOWN')
        dfs2(grid,(x+1,y),curr,height,width)
        curr.pop()
    if y-1>=0 and last != 'RIGHT':
        curr.append('LEFT')
        dfs2(grid,(x,y-1),curr,height,width)
        curr.pop()


def dfs(grid,pos):
    dfs2(grid,pos,['START'],len(grid),len(grid[0]))

########MAIN#########
#up, right, down, left
grid = [[0, 0, 0], [0, 2, 0], [1, 1, 0]]
start = (0,1)
print str(grid)
dfs(grid,start)
def dfs2(网格、位置、当前、高度、宽度): x=位置[0] y=位置[1] 如果网格[x][y]==1: 打印str(当前) 一无所获 elif网格[x][y]==2: 一无所获 最后一次=当前[-1] 如果x-1>=0且最后!='向下': 当前追加('UP') dfs2(栅格,(x-1,y),当前,高度,宽度) 流行音乐 如果y+1<宽度和最后一个!='左': 当前附加('右') dfs2(栅格,(x,y+1),当前,高度,宽度) 流行音乐 如果x+1<高度和最后一个!='向上': 当前追加('DOWN') dfs2(栅格,(x+1,y),当前,高度,宽度) 流行音乐 如果y-1>=0且最后!='对": 当前附加('左') dfs2(栅格,(x,y-1),当前,高度,宽度) 流行音乐 def dfs(网格、pos): dfs2(网格,位置,['START'],len(网格),len(网格[0])) ########主要######### #上、右、下、左 网格=[[0,0,0],[0,2,0],[1,1,0]] 开始=(0,1) 打印str(网格) dfs(网格,启动)
这是基于递归的。根据问题中指定的顺序尝试移动到下一个位置,并将移动存储在一个列表中,该列表在到达目的地时打印

是什么使路径终止于
(2,0)
?是否应在访问所有单元格或访问所有1或存在其他情况时终止?在尝试其他选项之前,机器人是否必须采取首选步骤?如果迷宫是
1 0*
正确答案是左、左还是无路径?当所有1都被访问时,路径终止,机器人按首选顺序执行步骤,因此如果迷宫是1 0 0*,它将尝试向上、向右、向下,但是这些都是不可能的移动,所以它会向左移动。你的问题解决了吗?我有点困惑这是如何工作的,代码给出了两个解决方案,都从开始位置开始,但是,一旦它得到第一个解决方案,它应该继续在相同的路径上,而不是从(0,1)开始。我在识别第二次从(0,1)开始的部件时遇到了问题实际上我已设法修复了该部件,但这是不对的,因为当它到达(2,1)时,它不会回溯,而是向左
,如果grid[x][y]==1:print str(curr)返回None
,那么它应该在找到解决方案后返回。你删除返回了吗?这是我的输出:[“开始”,“右”,“下”,“下”,“左”,“左”][“开始”,“左”,“下”,“下”]右,所以你的输出应该是[“开始”,“右”,“下”,“下”,“左”,“右”,“上”,“上”,“左”,“下”,“下”]
def dfs2(grid,pos,curr,height,width):
    x = pos[0]
    y = pos[1]    
    if grid[x][y]==1:
        print str(curr)
        return None
    elif grid[x][y] ==2:
        return None        
    last = curr[-1]    
    if x-1 >= 0 and last != 'DOWN':        
        curr.append('UP')
        dfs2(grid,(x-1,y),curr,height,width)
        curr.pop()
    if y+1 < width and last != 'LEFT':        
        curr.append('RIGHT')
        dfs2(grid,(x,y+1),curr,height,width)
        curr.pop()
    if x+1 < height and last != 'UP':
        curr.append('DOWN')
        dfs2(grid,(x+1,y),curr,height,width)
        curr.pop()
    if y-1>=0 and last != 'RIGHT':
        curr.append('LEFT')
        dfs2(grid,(x,y-1),curr,height,width)
        curr.pop()


def dfs(grid,pos):
    dfs2(grid,pos,['START'],len(grid),len(grid[0]))

########MAIN#########
#up, right, down, left
grid = [[0, 0, 0], [0, 2, 0], [1, 1, 0]]
start = (0,1)
print str(grid)
dfs(grid,start)