Possibility of moving out of maze | Java Possibility of moving out of maze ; Function to check whether it will stay inside or come out ; marks all the positions that is visited ; Initial starting point ; initial assumption is it comes out ; runs till it is inside or comes out ; if the movement is towards left then we move left . The start variable and mark that position as visited if not visited previously . Else we break out ; It will be inside forever ; If the movement is towards right , then we move right . The start variable and mark that position as visited if not visited previously else we break out ; it will be inside forever ; Driver code
import java . io . * ; class GFG { static void checkingPossibility ( int a [ ] , int n , String s ) { int mark [ ] = new int [ a [ 0 ] * n ] ; int start = 0 ; int possible = 1 ; while ( start >= 0 && start < n ) { if ( s == " < " ) { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start -= a [ start ] ; } else { possible = 0 ; break ; } } else { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start += a [ start ] ; } else { possible = 0 ; break ; } } } if ( possible == 0 ) System . out . print ( " it ▁ stays ▁ inside ▁ forever " ) ; else System . out . print ( " comes ▁ out " ) ; } public static void main ( String [ ] args ) { int n = 2 ; String s = " > < " ; int a [ ] = { 1 , 2 } ; checkingPossibility ( a , n , s ) ; } }