// Java program to find Longest Possible Route in a
// matrix with hurdles
using System;
class GFG {
static int R = 3;
static int C = 10;
// 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.
static Tuple<bool,int> findLongestPathUtil (int[, ] mat, int i, int j, int x, int y, bool[, ] visited) {
// if (i, j) itself is destination, return true
if(i == x && j == y)
return new Tuple<bool,int>(true, 0);
// if not a valid cell, return false
if(i < 0 || i >= R || j < 0 || j >= C || mat[i,j] == 0 || visited[i,j])
return new Tuple<bool,int>(false, Int32.MaxValue);
// include (i, j) in current path i.e.
// set visited(i, j) to true
visited[i,j] = true;
// res stores longest path from current cell (i, j) to
// destination cell (x, y)
int res = Int32.MinValue;
// go left from current cell
Tuple<bool,int> sol = findLongestPathUtil(mat, i, j-1, x, y, visited);
// if destination can be reached on going left from current
// cell, update res
if(sol.Item1)
res = Math.Max(sol.Item2, res);
// go right from current cell
sol = findLongestPathUtil(mat, i, j+1, x, y, visited);
// if destination can be reached on going right from current
// cell, update res
if(sol.Item1)
res = Math.Max(sol.Item2, res);
// go up from current cell
sol = findLongestPathUtil(mat, i-1, j, x, y, visited);
// if destination can be reached on going up from current
// cell, update res
if(sol.Item1)
res = Math.Max(sol.Item2, res);
// go down from current cell
sol = findLongestPathUtil(mat, i+1, j, x, y, visited);
// if destination can be reached on going down from current
// cell, update res
if(sol.Item1)
res = Math.Max(sol.Item2, res);
// Backtrack
visited[i,j] = false;
// if destination can be reached from current cell,
// return true
if(res != Int32.MinValue)
return new Tuple<bool,int>(true, res+1);
// if destination can't be reached from current cell,
// return false
else
return new Tuple<bool,int>(false, Int32.MaxValue);
}
// A wrapper function over findLongestPathUtil()
static void findLongestPath (int [, ]mat, int i, int j, int x, int y) {
// create a boolean matrix to store info about
// cells already visited in current route
bool[,] visited = new bool[R,C];
// find longest route from (i, j) to (x, y) and
// print its maximum cost
Tuple<bool,int> p = findLongestPathUtil(mat, i, j, x, y, visited);
if(p.Item1)
Console.WriteLine("Length of longest possible route is : " + p.Item2);
// If the destination is not reachable
else
Console.WriteLine("Destination not reachable from given source");
}
// Driver Code
public static void Main() {
// input matrix with hurdles shown with number 0
int[,] mat = new int[,] { { 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 } };
// find longest path with source (0, 0) and
// destination (1, 7)
findLongestPath(mat, 0, 0, 1, 7);
}
}
// This code is contributed by Abhijeet Kumar(abhijeet19403)