Longest Possible Route in a Matrix with Hurdles | Java program to find Longest Possible Route in a matrix with hurdles ; A Pair to store status of a cell . found is set to true of destination is reachable and value stores distance of longest path ; true if destination is found ; stores cost of longest path from current cell to destination cell ; Function to find Longest Possible Route in the matrix with hurdles . If the destination is not reachable the function returns false with cost Integer . MAX_VALUE . ( i , j ) is source cell and ( x , y ) is destination cell . ; if ( i , j ) itself is destination , return true ; if not a valid cell , return false ; include ( i , j ) in current path i . e . set visited ( i , j ) to true ; res stores longest path from current cell ( i , j ) to destination cell ( x , y ) ; go left from current cell ; if destination can be reached on going left from current cell , update res ; go right from current cell ; if destination can be reached on going right from current cell , update res ; go up from current cell ; if destination can be reached on going up from current cell , update res ; go down from current cell ; if destination can be reached on going down from current cell , update res ; Backtrack ; if destination can be reached from current cell , return true ; if destination can 't be reached from current cell,  return false ; A wrapper function over findLongestPathUtil ( ) ; create a boolean matrix to store info about cells already visited in current route ; find longest route from ( i , j ) to ( x , y ) and print its maximum cost ; If the destination is not reachable ; Driver Code ; input matrix with hurdles shown with number 0 ; find longest path with source ( 0 , 0 ) and destination ( 1 , 7 )
import java . io . * ; class GFG { static int R = 3 ; static int C = 10 ; static class Pair { boolean found ; int val ; Pair ( boolean x , int y ) { found = x ; val = y ; } } static Pair findLongestPathUtil ( int mat [ ] [ ] , int i , int j , int x , int y , boolean visited [ ] [ ] ) { if ( i == x && j == y ) return new Pair ( true , 0 ) ; if ( i < 0 i >= R j < 0 j >= C mat [ i ] [ j ] == 0 visited [ i ] [ j ] ) return new Pair ( false , Integer . MAX_VALUE ) ; visited [ i ] [ j ] = true ; int res = Integer . MIN_VALUE ; Pair sol = findLongestPathUtil ( mat , i , j - 1 , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; sol = findLongestPathUtil ( mat , i , j + 1 , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; sol = findLongestPathUtil ( mat , i - 1 , j , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; sol = findLongestPathUtil ( mat , i + 1 , j , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; visited [ i ] [ j ] = false ; if ( res != Integer . MIN_VALUE ) return new Pair ( true , res + 1 ) ; else return new Pair ( false , Integer . MAX_VALUE ) ; } static void findLongestPath ( int mat [ ] [ ] , int i , int j , int x , int y ) { boolean visited [ ] [ ] = new boolean [ R ] [ C ] ; Pair p = findLongestPathUtil ( mat , i , j , x , y , visited ) ; if ( p . found ) System . out . println ( " Length ▁ of ▁ longest ▁ possible ▁ route ▁ is ▁ " + p . val ) ; else System . out . println ( " Destination ▁ not ▁ reachable ▁ from ▁ given ▁ source " ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 1 } , { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } } ; findLongestPath ( mat , 0 , 0 , 1 , 7 ) ; } }