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