Print all possible paths from top left to bottom right of a mXn matrix | Python3 program to Print all possible paths from top left to bottom right of a mXn matrix ; if we reach the bottom of maze , we can only move right ; path . append ( maze [ i ] [ k ] ) ; if we hit this block , it means one path is completed . Add it to paths list and print ; if we reach to the right most corner , we can only move down ; path . append ( maze [ j ] [ k ] ) if we hit this block , it means one path is completed . Add it to paths list and print ; add current element to the path list path . append ( maze [ i ] [ j ] ) ; move down in y direction and call findPathsUtil recursively ; move down in y direction and call findPathsUtil recursively ; Driver code
allPaths = [ ] NEW_LINE def findPaths ( maze , m , n ) : NEW_LINE INDENT path = [ 0 for d in range ( m + n - 1 ) ] NEW_LINE findPathsUtil ( maze , m , n , 0 , 0 , path , 0 ) NEW_LINE DEDENT def findPathsUtil ( maze , m , n , i , j , path , indx ) : NEW_LINE INDENT global allPaths NEW_LINE if i == m - 1 : NEW_LINE INDENT for k in range ( j , n ) : NEW_LINE INDENT path [ indx + k - j ] = maze [ i ] [ k ] NEW_LINE DEDENT print ( path ) NEW_LINE allPaths . append ( path ) NEW_LINE return NEW_LINE DEDENT if j == n - 1 : NEW_LINE INDENT for k in range ( i , m ) : NEW_LINE INDENT path [ indx + k - i ] = maze [ k ] [ j ] NEW_LINE DEDENT print ( path ) NEW_LINE allPaths . append ( path ) NEW_LINE return NEW_LINE DEDENT path [ indx ] = maze [ i ] [ j ] NEW_LINE findPathsUtil ( maze , m , n , i + 1 , j , path , indx + 1 ) NEW_LINE findPathsUtil ( maze , m , n , i , j + 1 , path , indx + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT maze = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE findPaths ( maze , 3 , 3 ) NEW_LINE DEDENT