DFS path finding with obstacles
<p>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. The starting position has a * next to it.</p>

<pre><code> 0   0*  0
 0   2   0
 1   1   0
</code></pre>

<p>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.</p>

<p>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:</p>

<pre><code>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 &gt; 0: 
          routes[(row, col)].append((row , col - 1))
      # Down moves    
     if row &lt; height - 1:
          routes[(row, col)].append(((row + 1, col)))
     # Right moves
     if col &lt; width - 1: 
         routes[(row, col)].append(((row , col + 1)))
     # Up moves
     if row &gt; 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
</code></pre>

<p>Where</p>

<pre><code> grid = [[0, 0, 0], [0, 2, 0], [1, 1, 0]]
 start = (0,1)
</code></pre>

<p>Doing this by hand shows that the path should go as follows: right, down, down, left, right, up, up, left, left, down, down</p>

<p>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</p>

<p>Try this:</p>

<pre><code>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 &gt;= 0 and last != 'DOWN':        
        curr.append('UP')
        dfs2(grid,(x-1,y),curr,height,width)
        curr.pop()
    if y+1 &lt; width and last != 'LEFT':        
        curr.append('RIGHT')
        dfs2(grid,(x,y+1),curr,height,width)
        curr.pop()
    if x+1 &lt; height and last != 'UP':
        curr.append('DOWN')
        dfs2(grid,(x+1,y),curr,height,width)
        curr.pop()
    if y-1&gt;=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)
</code></pre>

<p>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</p>

python artificial-intelligence depth-first-search