Shortest path from a source cell to a destination cell of a Binary Matrix through cells consisting only of 1 s | Python3 program for the above approach ; Stores the coordinates of the matrix cell ; Stores coordinates of a cell and its distance ; Check if the given cell is valid or not ; Stores the moves of the directions of adjacent cells ; Function to find the shortest path from the source to destination in the given matrix ; Stores the distance for each cell from the source cell ; Distance of source cell is 0 ; Initialize a visited array ; Mark source cell as visited ; Create a queue for BFS ; Distance of source cell is 0 ; Enqueue source cell ; Keeps track of whether destination is reached or not ; Iterate until queue is not empty ; Deque front of the queue ; If the destination cell is reached , then find the path ; Assign the distance of destination to the distance matrix ; Stores the smallest path ; Iterate until source is reached ; Append D ; Append U ; Append R ; Append L ; Reverse the backtracked path ; Explore all adjacent directions ; If the current cell is valid cell and can be traversed ; Mark the adjacent cells as visited ; Enque the adjacent cells ; Update the distance of the adjacent cells ; If the destination is not reachable ; Driver Code
from collections import deque NEW_LINE class Point : NEW_LINE INDENT def __init__ ( self , xx , yy ) : NEW_LINE INDENT self . x = xx NEW_LINE self . y = yy NEW_LINE DEDENT DEDENT class Node : NEW_LINE INDENT def __init__ ( self , P , d ) : NEW_LINE INDENT self . pt = P NEW_LINE self . dist = d NEW_LINE DEDENT DEDENT def isValid ( row , col ) : NEW_LINE INDENT return ( row >= 0 ) and ( col >= 0 ) and ( row < 4 ) and ( col < 4 ) NEW_LINE DEDENT dRow = [ - 1 , 0 , 0 , 1 ] NEW_LINE dCol = [ 0 , - 1 , 1 , 0 ] NEW_LINE def pathMoves ( mat , src , dest ) : NEW_LINE INDENT d = [ [ - 1 for i in range ( 4 ) ] for i in range ( 4 ) ] NEW_LINE d [ src . x ] [ src . y ] = 0 NEW_LINE visited = [ [ False for i in range ( 4 ) ] for i in range ( 4 ) ] NEW_LINE visited [ src . x ] [ src . y ] = True NEW_LINE q = deque ( ) NEW_LINE s = Node ( src , 0 ) NEW_LINE q . append ( s ) NEW_LINE ok = False NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT curr = q . popleft ( ) NEW_LINE pt = curr . pt NEW_LINE if ( pt . x == dest . x and pt . y == dest . y ) : NEW_LINE INDENT xx , yy = pt . x , pt . y NEW_LINE dist = curr . dist NEW_LINE d [ pt . x ] [ pt . y ] = dist NEW_LINE pathmoves = " " NEW_LINE while ( xx != src . x or yy != src . y ) : NEW_LINE INDENT if ( xx > 0 and d [ xx - 1 ] [ yy ] == dist - 1 ) : NEW_LINE INDENT pathmoves += ' D ' NEW_LINE xx -= 1 NEW_LINE DEDENT if ( xx < 4 - 1 and d [ xx + 1 ] [ yy ] == dist - 1 ) : NEW_LINE INDENT pathmoves += ' U ' NEW_LINE xx += 1 NEW_LINE DEDENT if ( yy > 0 and d [ xx ] [ yy - 1 ] == dist - 1 ) : NEW_LINE INDENT pathmoves += ' R ' NEW_LINE yy -= 1 NEW_LINE DEDENT if ( yy < 4 - 1 and d [ xx ] [ yy + 1 ] == dist - 1 ) : NEW_LINE INDENT pathmoves += ' L ' NEW_LINE yy += 1 NEW_LINE DEDENT dist -= 1 NEW_LINE DEDENT pathmoves = pathmoves [ : : - 1 ] NEW_LINE print ( pathmoves , end = " " ) NEW_LINE ok = True NEW_LINE break NEW_LINE DEDENT for i in range ( 4 ) : NEW_LINE INDENT row = pt . x + dRow [ i ] NEW_LINE col = pt . y + dCol [ i ] NEW_LINE if ( isValid ( row , col ) and ( mat [ row ] [ col ] == '1' or mat [ row ] [ col ] == ' s ' or mat [ row ] [ col ] == ' d ' ) and ( not visited [ row ] [ col ] ) ) : NEW_LINE INDENT visited [ row ] [ col ] = True NEW_LINE adjCell = Node ( Point ( row , col ) , curr . dist + 1 ) NEW_LINE q . append ( adjCell ) NEW_LINE d [ row ] [ col ] = curr . dist + 1 NEW_LINE DEDENT DEDENT DEDENT if ( not ok ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ '0' , '1' , '0' , '1' ] , [ '1' , '0' , '1' , '1' ] , [ '0' , '1' , '1' , '1' ] , [ '1' , '1' , '1' , '0' ] ] NEW_LINE src = Point ( 0 , 3 ) NEW_LINE dest = Point ( 3 , 0 ) NEW_LINE pathMoves ( mat , src , dest ) NEW_LINE DEDENT