title: How to handle or detect when a maze has no solution?
body: <p>I am trying to make maze solver in python.</p>
  
  <p>The parameters are as follows </p>
  
  <p>y (int): Start location of y</p>
  
  <p>x (int): Start location of x</p>
  
  <p>The starting point is (y,x), The coordinates for the left-top, left-bottom, right-top, and right-bottom are (0,0), (N,0), (0,N), (N,N), respectively, for the maze map of size N by N.</p>
  
  <p>The search function return (bool): If maze has solution or not, return True or False respectively.</p>
  
  <p>The solve function returns nothing: Print out a single path solution.</p>
  
  <p>I have managed to write the code and get solutions for the maze, but I have a problem when the maze has no solution. It does not stop and just goes on running infinite loops. </p>
  
  <p>Below is my code</p>
  
  <pre><code>class Maze():
      def __init__(self, grid):
  
          self.grid = grid
  
      def search(self, y, x):
  
          self.y = y
          self.x = x
  
          if x == len(self.grid[0]) or y == len(self.grid):
              print('out of maze')
              return False
          if self.grid[y][x] == 'F':
              self.cleanup()
              return True
          if self.grid[y][x] != 'o' and self.grid[y][x] != ' ':
              print('ran into a wall')
              return False
  
          self.grid[y][x] = ' '
  
          if x &lt;= len(self.grid[0])-2:
              if self.grid[y][x+1] == 'o' or self.grid[y][x+1] == 'F':
                  print(x,y)
                  return self.search(y,x+1)
          if y &lt;= len(self.grid)-2:
              if self.grid[y+1][x] == 'o' or self.grid[y+1][x] == 'F':
                  print(x,y)
                  return self.search(y+1,x)
          if x &gt; 1:
              if self.grid[y][x-1] == 'o' or self.grid[y][x-1] == 'F':
                  print(x,y)
                  return self.search(y,x-1)
          if y &gt; 1:
              if self.grid[y-1][x] == 'o' or self.grid[y-1][x] == 'F':
                  print(x,y)
                  return self.search(y-1,x) 
  
  
          if x &gt; 1:
              if self.grid[y][x-1] == ' ' or self.grid[y][x-1] == 'F':
                  print(x,y)
                  return self.search(y,x-1)
          if y &gt; 1:
              if self.grid[y-1][x] == ' ' or self.grid[y-1][x] == 'F':
                  print(x,y)
                  return self.search(y-1,x)
          if x &lt;= len(self.grid[0])-2:
              if self.grid[y][x+1] == ' ' or self.grid[y][x+1] == 'F':
                  print(x,y)
                  return self.search(y,x+1)
          if y &lt;= len(self.grid)-2:
              if self.grid[y+1][x] == ' ' or self.grid[y+1][x] == 'F':
                  print(x,y)
                  return self.search(y+1,x)
  
          print("can't find a way")
          return False
  
      def cleanup(self):
          for r in range(len(self.grid)):
              for c in range(len(self.grid[0])):
                  if self.grid[r][c] == ' ':
                      self.grid[r][c] = 'o'
  
      def solve(self, y, x):
  
          if self.search(y, x):
              print("Solution")
              ### CODE HERE ###
          else:
              print('There are NO solutions')
  
  </code></pre>
  
  <p>This is my main </p>
  
  <pre><code>for i, maze_map in enumerate(maze_map_list):  
      maze = Maze(maze_map)
      print(i+1, '-1')
      maze.solve(0,0)
      print(i+1, '-2')
      maze.solve(4,0)
      print()
  </code></pre>
  
  <p>and this is the grid that I am unable to work on </p>
  
  <pre><code>.
  .
  grid2 = ['o o o o o o'.split(' '),
           '+ + o + + +'.split(' '),
           'F + o o o o'.split(' '),
           'o o + + + o'.split(' '),
           'o o o + + o'.split(' ')] 
  .
  .
  .
  maze_map_list = [grid1, grid2, grid3, grid4]
  </code></pre>