Shortest path from a source cell to a destination cell of a Binary Matrix through cells consisting only of 1 s | Javascript 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 ; push 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 ; Print reverse the backtracked path ; Pop the start of queue ; 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
var ROW = 0 ; var COL = 0 ; class Point { constructor ( ) { this . x = 0 ; this . y = 0 ; } } ; function newPoint ( x , y ) { var temp = new Point ( ) ; temp . x = x ; temp . y = y ; return temp ; } class Node { constructor ( ) { this . pt = null ; this . dist = 0 ; } } ; function newNode ( p , dist ) { var temp = new Node ( ) ; temp . pt = p ; temp . dist = dist ; return temp ; } function isValid ( row , col ) { return ( row >= 0 ) && ( col >= 0 ) && ( row < ROW ) && ( col < COL ) ; } var dRow = [ - 1 , 0 , 0 , 1 ] ; var dCol = [ 0 , - 1 , 1 , 0 ] ; function pathMoves ( mat , src , dest ) { var d = Array . from ( Array ( ROW ) , ( ) => Array ( COL ) ) ; for ( var i = 0 ; i < ROW ; i ++ ) { for ( var j = 0 ; j < COL ; j ++ ) d [ i ] [ j ] = - 1 ; } d [ src . x ] [ src . y ] = 0 ; var visited = Array . from ( Array ( ROW ) , ( ) => Array ( COL ) ) ; visited [ src . x ] [ src . y ] = true ; var q = [ ] ; var s = newNode ( src , 0 ) ; q . push ( s ) ; var ok = false ; while ( q . length > 0 ) { var curr = q [ 0 ] ; q . shift ( ) ; var pt = curr . pt ; if ( pt . x == dest . x && pt . y == dest . y ) { var xx = pt . x , yy = pt . y ; var dist = curr . dist ; d [ pt . x ] [ pt . y ] = dist ; var pathmoves = " " ; while ( xx != src . x yy != src . y ) { if ( xx > 0 && d [ xx - 1 ] [ yy ] == dist - 1 ) { pathmoves += ' ' ; xx -- ; } if ( xx < ROW - 1 && d [ xx + 1 ] [ yy ] == dist - 1 ) { pathmoves += ' ' ; xx ++ ; } if ( yy > 0 && d [ xx ] [ yy - 1 ] == dist - 1 ) { pathmoves += ' ' ; yy -- ; } if ( yy < COL - 1 && d [ xx ] [ yy + 1 ] == dist - 1 ) { pathmoves += ' ' ; yy ++ ; } dist -- ; } for ( var i = pathmoves . length - 1 ; i >= 0 ; -- i ) document . write ( pathmoves [ i ] ) ; ok = true ; break ; } if ( q . length > 0 ) { q . shift ( ) ; } for ( var i = 0 ; i < 4 ; i ++ ) { var row = pt . x + dRow [ i ] ; var col = pt . y + dCol [ i ] ; if ( isValid ( row , col ) && ( mat [ row ] [ col ] == ' ' mat [ row ] [ col ] == ' ' mat [ row ] [ col ] == ' ' ) && ! visited [ row ] [ col ] ) { visited [ row ] [ col ] = true ; var adjCell = newNode ( newPoint ( row , col ) , curr . dist + 1 ) ; q . push ( adjCell ) ; d [ row ] [ col ] = curr . dist + 1 ; } } } if ( ok == false ) document . write ( - 1 ) ; } var mat = [ [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] ] ; ROW = mat . length ; COL = mat [ 0 ] . length ; var src = newPoint ( 0 , 3 ) ; var dest = newPoint ( 3 , 0 ) ; pathMoves ( mat , src , dest ) ;