Check for possible path in 2D matrix | C # program to find if there is path from top left to right bottom ; To find the path from top left to bottom right ; Directions ; Queue ; Insert the top right corner . ; Until queue is empty ; Mark as visited ; Destination is reached . ; Check all four directions ; Using the direction array ; Not blocked and valid ; Driver Code ; Given array ; Path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]
using System ; using System . Collections ; class GFG { static int row = 5 ; static int col = 5 ; static bool isPath ( int [ , ] arr ) { int [ , ] dir = { { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } , { - 1 , 0 } } ; Queue q = new Queue ( ) ; q . Enqueue ( new Tuple < int , int > ( 0 , 0 ) ) ; while ( q . Count > 0 ) { Tuple < int , int > p = ( Tuple < int , int > ) ( q . Peek ( ) ) ; q . Dequeue ( ) ; arr [ p . Item1 , p . Item2 ] = - 1 ; if ( p == new Tuple < int , int > ( row - 1 , col - 1 ) ) return true ; for ( int i = 0 ; i < 4 ; i ++ ) { int a = p . Item1 + dir [ i , 0 ] ; int b = p . Item2 + dir [ i , 1 ] ; if ( a >= 0 && b >= 0 && a < row && b < col && arr [ a , b ] != - 1 ) { q . Enqueue ( new Tuple < int , int > ( a , b ) ) ; } } } return false ; } static void Main ( ) { int [ , ] arr = { { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , 0 , - 1 , - 1 } , { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , - 1 , 0 , - 1 } , { 0 , 0 , - 1 , 0 , 0 } } ; if ( isPath ( arr ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }