Shortest path in a Binary Maze | Python program to find the shortest path between a given source cell to a destination cell . ; To store matrix cell cordinates ; A data structure for queue used in BFS ; The cordinates of the cell ; Cell 's distance from the source ; Check whether given cell ( row , col ) is a valid cell or not ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 4 neighbours of a given cell ; Function to find the shortest path between a given source cell to a destination cell . ; check source and destination cell of the matrix have value 1 ; Mark the source cell as visited ; Create a queue for BFS ; Distance of source cell is 0 ; Enqueue source cell ; Do a BFS starting from source cell ; If we have reached the destination cell , we are done ; Otherwise enqueue its adjacent cells ; if adjacent cell is valid , has path and not visited yet , enqueue it . ; mark cell as visited and enqueue it ; Return - 1 if destination cannot be reached ; Driver code
from collections import deque NEW_LINE ROW = 9 NEW_LINE COL = 10 NEW_LINE class Point : NEW_LINE INDENT def __init__ ( self , x : int , y : int ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT class queueNode : NEW_LINE INDENT def __init__ ( self , pt : Point , dist : int ) : NEW_LINE INDENT self . pt = pt NEW_LINE self . dist = dist NEW_LINE DEDENT DEDENT def isValid ( row : int , col : int ) : NEW_LINE INDENT return ( row >= 0 ) and ( row < ROW ) and ( col >= 0 ) and ( col < COL ) NEW_LINE DEDENT rowNum = [ - 1 , 0 , 0 , 1 ] NEW_LINE colNum = [ 0 , - 1 , 1 , 0 ] NEW_LINE def BFS ( mat , src : Point , dest : Point ) : NEW_LINE INDENT if mat [ src . x ] [ src . y ] != 1 or mat [ dest . x ] [ dest . y ] != 1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT visited = [ [ False for i in range ( COL ) ] for j in range ( ROW ) ] NEW_LINE visited [ src . x ] [ src . y ] = True NEW_LINE q = deque ( ) NEW_LINE s = queueNode ( src , 0 ) NEW_LINE q . append ( s ) NEW_LINE while q : 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 return curr . dist NEW_LINE DEDENT for i in range ( 4 ) : NEW_LINE INDENT row = pt . x + rowNum [ i ] NEW_LINE col = pt . y + colNum [ i ] NEW_LINE if ( isValid ( row , col ) and mat [ row ] [ col ] == 1 and not visited [ row ] [ col ] ) : NEW_LINE INDENT visited [ row ] [ col ] = True NEW_LINE Adjcell = queueNode ( Point ( row , col ) , curr . dist + 1 ) NEW_LINE q . append ( Adjcell ) NEW_LINE DEDENT DEDENT DEDENT return - 1 NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT mat = [ [ 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] , [ 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 ] , [ 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 ] ] NEW_LINE source = Point ( 0 , 0 ) NEW_LINE dest = Point ( 3 , 4 ) NEW_LINE dist = BFS ( mat , source , dest ) NEW_LINE if dist != - 1 : NEW_LINE INDENT print ( " Shortest ▁ Path ▁ is " , dist ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Shortest ▁ Path ▁ doesn ' t ▁ exist " ) NEW_LINE DEDENT DEDENT main ( ) NEW_LINE