Maze With N doors and 1 Key | Recursive function to check whether there is a path from the top left cell to the bottom right cell of the maze ; Check whether the current cell is within the maze ; If key is required to move further ; If the key hasn 't been used before ; If current cell is the destination ; Either go down or right ; Key has been used before ; If current cell is the destination ; Either go down or right ; Driver code ; If there is a path from the cell ( 0 , 0 )
function findPath ( maze , xpos , ypos , key ) { if ( xpos < 0 xpos >= maze . length ypos < 0 ypos >= maze . length ) return false ; if ( maze [ xpos ] [ ypos ] == ' ' ) { if ( key == true ) if ( xpos == maze . length - 1 && ypos == maze . length - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; return false ; } if ( xpos == maze . length - 1 && ypos == maze . length - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } function mazeProb ( maze , xpos , ypos ) { let key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } let maze = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let n = maze . length ; if ( mazeProb ( maze , 0 , 0 ) ) document . write ( " " ) ; else document . write ( " " ) ;