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
def checkingPossibility ( a , n , s ) : NEW_LINE INDENT mark = [ 0 ] * n NEW_LINE start = 0 NEW_LINE possible = 1 NEW_LINE while start >= 0 and start < n : NEW_LINE INDENT if s [ start ] == " < " : NEW_LINE INDENT if mark [ start ] == 0 : NEW_LINE INDENT mark [ start ] = 1 NEW_LINE start -= a [ start ] NEW_LINE DEDENT else : NEW_LINE INDENT possible = 0 NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if mark [ start ] == 0 : NEW_LINE INDENT mark [ start ] = 1 NEW_LINE start += a [ start ] NEW_LINE DEDENT else : NEW_LINE INDENT possible = 0 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if possible == 0 : NEW_LINE INDENT print " it ▁ stays ▁ inside ▁ forever " NEW_LINE DEDENT else : NEW_LINE INDENT print " comes ▁ out " NEW_LINE DEDENT DEDENT n = 2 NEW_LINE s = " > < " NEW_LINE a = [ 1 , 2 ] NEW_LINE checkingPossibility ( a , n , s ) NEW_LINE