title: Path finding issue caused due to return statement in recursion
body: <p>I am trying to solve the following wall problem (on matrix) with 3 type of queries:</p>
  
  <blockquote>
    <p>1 x y</p>
  </blockquote>
  
  <ul>
  <li>build the wall between cells (x, y) and (x, y+1). If there is already exist a wall between them, this query is ignored.</li>
  </ul>
  
  <blockquote>
    <p>2 x y</p>
  </blockquote>
  
  <ul>
  <li>build the wall between cells (x, y) and (x+1, y). If there is already exist a wall between them, this query is ignored.</li>
  </ul>
  
  <blockquote>
    <p>3 x1 y1 x2 y2</p>
  </blockquote>
  
  <ul>
  <li>check if cells (x1, y1) and (x2, y2) are connected. You must answer 1 if they are connected, 0 otherwise.</li>
  </ul>
  
  <p>You can move only right or bottom if there is <strong>no wall</strong>.</p>
  
  <p>I have coded it like this:</p>
  
  <pre><code>#Declarations and Assignments
  
  #q = number of above listed queries to execute
  
  rows,cols,q = (int(x) for x in input().split())
  mat = [[None]*cols for _ in range(rows)]
  
  class cell:
      def __init__(Self):
          Self.wr = False #Wall to right
          Self.wb = False #Wall at bottom
  
  #safety conition i.e. checking walls
  
  def isSafe(mat,oper,r,c):
      if(r&gt;=rows or c&gt;=cols):
          return False
      elif(oper == 0 and mat[r][c].wr == True):
          return False
      elif(oper == 1 and mat[r][c].wb == True):
          return False
      else:
          return True
  
  #Recursivepath finder
  
  def path(mat,x1,y1,x2,y2):
      #print('path from', x1,y1)
      if(x1 == x2 and y1 == y2):
          return True
      elif(x1 &gt; x2 and y1 &gt; y2):
          return False
      else:
          if(isSafe(mat,0,x1,y1)):
              path(mat,x1,y1+1,x2,y2)
          if(isSafe(mat,1,x1,y1)):
              path(mat,x1+1,y1,x2,y2)
          return
  
  #For prining the matrix
  
  def printMat(mat):
      for i in range(0,rows):
          for j in range(0,cols):
              print('For',i,j,':',mat[i][j].wr , ' ', mat[i][j].wb,end="\t")
          print('\n')
  
  #Forming Matrix
  
  for i in range(0,rows):
      for j in range(0,cols):
          mat[i][j] = cell()
  
  #Taking the q queries
  
  for i in range(0,q):
      t = [int(i) for i in input().split()]
      if(t[0] == 1):
          mat[t[1]][t[2]].wr = True
      elif(t[0] == 2):
          mat[t[1]][t[2]].wb = True
      elif(t[0] == 3):
          #print('Path from', t[1],t[2],'to',t[3],t[4])
          if(path(mat,t[1],t[2],t[3],t[4])):
              print(1)
          else: print(0)
  </code></pre>
  
  <p>let the sample input be: </p>
  
  <p>4 5 7
   #(A 4x5 matrix and 7 queries)</p>
  
  <p>2 1 0</p>
  
  <p>1 1 1</p>
  
  <p>1 2 1</p>
  
  <p>2 2 0</p>
  
  <p>2 2 1</p>
  
  <p>1 0 1</p>
  
  <p>3 0 0 2 1</p>
  
  <p>On execution, I can see that I have already reached 2,1 from 0,0. Also, the function finds all the paths possible to 2,1 but the final result always prints as 0 instead of 1. That means the path() (Despite of reaching the target node and finding all possible paths) does not return true. This is probably returning in a recursion issue. Can someone please solve this problem and explain <strong>return statement in recursion</strong>?</p>