简体   繁体   English

有障碍物的 DFS 寻路

[英]DFS path finding with obstacles

I have the following grid and i'm trying to get to the 1's in that grid, the obstacle is shown by 2. The robot has prioritized movement in this order: up, right, down, left.我有以下网格,我正在尝试到达该网格中的 1,障碍物显示为 2。机器人按以下顺序优先移动:向上、向右、向下、向左。 The starting position has a * next to it.起始位置旁边有一个 *。

 0   0*  0
 0   2   0
 1   1   0

What i have done so far is to put the position of that grid into a graph form, and also generated all the possible moves from each position in the prioritized order.到目前为止,我所做的是将该网格的位置放入图形形式中,并按照优先顺序从每个位置生成所有可能的移动。 But my issue is that the algorithm gets stuck in a loop when it gets to the last section of the second row.但我的问题是算法在到达第二行的最后一部分时陷入循环。 I'm trying to implement some sort of a cycle detector so i don't get stuck in that loop.我正在尝试实现某种循环检测器,这样我就不会陷入该循环中。

I should also mention that the robot can visit the same position twice as long as it's through different paths.我还应该提到,只要机器人通过不同的路径,它就可以两次访问同一位置。 So what I have so far is:所以到目前为止我所拥有的是:

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

Where在哪里

 grid = [[0, 0, 0], [0, 2, 0], [1, 1, 0]]
 start = (0,1)

Doing this by hand shows that the path should go as follows: right, down, down, left, right, up, up, left, left, down, down手动执行此操作表明路径应如下所示:右、下、下、左、右、上、上、左、左、下、下

any suggestions on how i can implement the detector would be great, I'm very new to AI and python and i've been trying to figure this out all day...thank you关于如何实现检测器的任何建议都会很棒,我对 AI 和 python 很陌生,我一整天都在试图解决这个问题……谢谢

Try this:尝试这个:

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)

This is based on recursion.这是基于递归的。 Attemps to move to the next position based on the order specified in the question and stores the movements in a list which gets printed on reaching the destination尝试根据问题中指定的顺序移动到下一个位置,并将移动存储在到达目的地时打印的列表中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM