human
Print all paths from a source point to all the 4 corners of a Matrix | Function to check if we reached on of the entry / exit ( corner ) point ..
Function to check if the index is within the matrix boundary ..
Recursive helper function.
If any corner is reached push the string t into ans and return.
For all the four directions.
The new ith index.
The new jth index.
The direction R / L / U / D.
If the new cell is within the matrix boundary and it is not previously visited in same path.
mark the new cell visited.
Store the direction.
Backtrack to explore other paths.
Function to find all possible paths.
Create a direction array for all the four directions.
stores the result.
Initialise variable.
function call.
Print the result"
Solution in Python:
assistant
#Here is code to print all paths from a source point to all the 4 corners of a Matrix and the other steps required.
 def isCorner ( i , j , M , N ) :
	if ( ( i == 0 and j == 0 ) or ( i == 0 and j == N - 1 ) or ( i == M - 1 and j == N - 1 ) or ( i == M - 1 and j == 0 ) ) :
		return True
	return False
def isValid ( i , j , M , N ) :
	if ( i < 0 or i >= M or j < 0 or j >= N ) :
		return False
	return True
def solve ( i , j , M , N , Dir , maze , t , ans ) :
	if ( isCorner ( i , j , M , N ) ) :
		ans . append ( t )
		return
	for k in range ( 4 ) :
		x = i + Dir [ k ] [ 0 ]
		y = j + Dir [ k ] [ 1 ]
		c = Dir [ k ] [ 2 ]
		if ( isValid ( x , y , M , N ) and maze [ x ] [ y ] == 1 ) :
			maze [ x ] [ y ] = 0
			t += c
			solve ( x , y , M , N , Dir , maze , t , ans )
			t = t [ : len ( t ) - 1 ]
			maze [ x ] [ y ] = 1
	return
def possiblePaths ( src , maze ) :
	Dir = [ [ - 1 , 0 , ' U ' ] , [ 0 , 1 , ' R ' ] , [ 1 , 0 , ' D ' ] , [ 0 , - 1 , ' L ' ] ]
	temp = " "
	ans = [ ]
	solve ( src [ 0 ] , src [ 1 ] , len ( maze ) , len ( maze [ 0 ] ) , Dir , maze , temp , ans )
	return ans
maze = [ [ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 ] , [ 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 ] , [ 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 ] ]
src = [ 4 , 2 ]
paths = possiblePaths ( src , maze )
if ( len ( paths ) == 0 ) :
	print ( " No ▁ Possible ▁ Paths " )
else :
	for i in paths :
		print ( i )