Function to find maximum length Snake sequence path ( i , j ) corresponds to tail of the snake
list < Point > findPath ( int grid [ M ] [ N ] , int mat [ M ] [ N ] , int i , int j ) { list < Point > path ; Point pt = { i , j } ; path . push_front ( pt ) ; while ( grid [ i ] [ j ] != 0 ) { if ( i > 0 && grid [ i ] [ j ] - 1 == grid [ i - 1 ] [ j ] ) { pt = { i - 1 , j } ; path . push_front ( pt ) ; i -- ; } else if ( j > 0 && grid [ i ] [ j ] - 1 == grid [ i ] [ j - 1 ] ) { pt = { i , j - 1 } ; path . push_front ( pt ) ; j -- ; } } return path ; }