Longest Possible Route in a Matrix with Hurdles | C ++ 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 INT_MAX . ( 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 ; initialize visited to false ; 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 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  3 NEW_LINE #define C  10 NEW_LINE struct Pair { bool found ; int value ; } ; Pair findLongestPathUtil ( int mat [ R ] [ C ] , int i , int j , int x , int y , bool visited [ R ] [ C ] ) { if ( i == x && j == y ) { Pair p = { true , 0 } ; return p ; } if ( i < 0 i > = R j < 0 j > = C mat [ i ] [ j ] == 0 visited [ i ] [ j ] ) { Pair p = { false , INT_MAX } ; return p ; } visited [ i ] [ j ] = true ; int res = INT_MIN ; Pair sol = findLongestPathUtil ( mat , i , j - 1 , x , y , visited ) ; if ( sol . found ) res = max ( res , sol . value ) ; sol = findLongestPathUtil ( mat , i , j + 1 , x , y , visited ) ; if ( sol . found ) res = max ( res , sol . value ) ; sol = findLongestPathUtil ( mat , i - 1 , j , x , y , visited ) ; if ( sol . found ) res = max ( res , sol . value ) ; sol = findLongestPathUtil ( mat , i + 1 , j , x , y , visited ) ; if ( sol . found ) res = max ( res , sol . value ) ; visited [ i ] [ j ] = false ; if ( res != INT_MIN ) { Pair p = { true , 1 + res } ; return p ; } else { Pair p = { false , INT_MAX } ; return p ; } } void findLongestPath ( int mat [ R ] [ C ] , int i , int j , int x , int y ) { bool visited [ R ] [ C ] ; memset ( visited , false , sizeof visited ) ; Pair p = findLongestPathUtil ( mat , i , j , x , y , visited ) ; if ( p . found ) cout << " Length ▁ of ▁ longest ▁ possible ▁ route ▁ is ▁ " << p . value ; else cout << " Destination ▁ not ▁ reachable ▁ from ▁ given ▁ " " source " ; } int main ( ) { int mat [ R ] [ C ] = { { 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 ) ; return 0 ; }