Maze With N doors and 1 Key | C # implementation of the approach ; 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 ; If current cell is the destination ; Either go down or right ; Driver code ; We are making these { { '0' , '0' , '1' } , { '1' , '0' , '1' } , { '1' , '1' , '0' } } ; ; If there is a path from the cell ( 0 , 0 )
using System ; using System . Collections . Generic ; class GFG { static bool findPath ( List < List < int > > maze , int xpos , int ypos , bool key ) { if ( xpos < 0 xpos >= maze . Count ypos < 0 ypos >= maze . Count ) return false ; if ( maze [ xpos ] [ ypos ] == '1' ) { if ( key == true ) if ( xpos == maze . Count - 1 && ypos == maze . Count - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; } if ( xpos == maze . Count - 1 && ypos == maze . Count - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } static bool mazeProb ( List < List < int > > maze , int xpos , int ypos ) { bool key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } public static void Main ( String [ ] args ) { int size = 3 ; List < List < int > > maze = new List < List < int > > ( size ) ; for ( int i = 0 ; i < size ; i ++ ) { maze . Add ( new List < int > ( ) ) ; } maze [ 0 ] . Add ( 0 ) ; maze [ 0 ] . Add ( 0 ) ; maze [ 0 ] . Add ( 1 ) ; maze [ 1 ] . Add ( 1 ) ; maze [ 1 ] . Add ( 0 ) ; maze [ 1 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 0 ) ; if ( mazeProb ( maze , 0 , 0 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }