Given an M x N matrix, with a few hurdles arbitrarily placed, calculate the length of longest possible route possible from source to destination within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contains any diagonal moves and a location once visited in a particular path cannot be visited again.

For example, longest path with no hurdles from source to destination is highlighted for below matrix. The length of the path is 24.

The idea is to use Backtracking. We start from the source cell of the matrix, move forward in all four allowed directions and recursively checks if they leads to the solution or not. If destination is found, we update the value of longest path else if none of the above solutions work we return false from our function.

Below is C++ implementation of above idea –

`// C++ program to find Longest Possible Route in a ` `// matrix with hurdles ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define R 3 ` `#define C 10 ` ` ` `// A Pair to store status of a cell. found is set to ` `// true of destination is reachable and value stores ` `// distance of longest path ` `struct` `Pair ` `{ ` ` ` `// true if destination is found ` ` ` `bool` `found; ` ` ` ` ` `// stores cost of longest path from current cell to ` ` ` `// destination cell ` ` ` `int` `value; ` `}; ` ` ` `// 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. ` `Pair findLongestPathUtil(` `int` `mat[R][C], ` `int` `i, ` `int` `j, ` ` ` `int` `x, ` `int` `y, ` `bool` `visited[R][C]) ` `{ ` ` ` ` ` `// if (i, j) itself is destination, return true ` ` ` `if` `(i == x && j == y) ` ` ` `{ ` ` ` `Pair p = { ` `true` `, 0 }; ` ` ` `return` `p; ` ` ` `} ` ` ` ` ` `// if not a valid cell, return false ` ` ` `if` `(i < 0 || i >= R || j < 0 || j >= C || ` ` ` `mat[i][j] == 0 || visited[i][j]) ` ` ` `{ ` ` ` `Pair p = { ` `false` `, INT_MAX }; ` ` ` `return` `p; ` ` ` `} ` ` ` ` ` `// 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 = INT_MIN; ` ` ` ` ` `// go left from current cell ` ` ` `Pair sol = findLongestPathUtil(mat, i, j - 1, x, y, visited); ` ` ` ` ` `// if destination can be reached on going left from current ` ` ` `// cell, update res ` ` ` `if` `(sol.found) ` ` ` `res = max(res, sol.value); ` ` ` ` ` `// 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.found) ` ` ` `res = max(res, sol.value); ` ` ` ` ` `// 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.found) ` ` ` `res = max(res, sol.value); ` ` ` ` ` `// 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.found) ` ` ` `res = max(res, sol.value); ` ` ` ` ` `// Backtrack ` ` ` `visited[i][j] = ` `false` `; ` ` ` ` ` `// if destination can be reached from current cell, ` ` ` `// return true ` ` ` `if` `(res != INT_MIN) ` ` ` `{ ` ` ` `Pair p = { ` `true` `, 1 + res }; ` ` ` `return` `p; ` ` ` `} ` ` ` ` ` `// if destination can't be reached from current cell, ` ` ` `// return false ` ` ` `else` ` ` `{ ` ` ` `Pair p = { ` `false` `, INT_MAX }; ` ` ` `return` `p; ` ` ` `} ` `} ` ` ` `// A wrapper function over findLongestPathUtil() ` `void` `findLongestPath(` `int` `mat[R][C], ` `int` `i, ` `int` `j, ` `int` `x, ` ` ` `int` `y) ` `{ ` ` ` `// create a boolean matrix to store info about ` ` ` `// cells already visited in current route ` ` ` `bool` `visited[R][C]; ` ` ` ` ` `// initailize visited to false ` ` ` `memset` `(visited, ` `false` `, ` `sizeof` `visited); ` ` ` ` ` `// find longest route from (i, j) to (x, y) and ` ` ` `// print its maximum cost ` ` ` `Pair p = findLongestPathUtil(mat, i, j, x, y, visited); ` ` ` `if` `(p.found) ` ` ` `cout << ` `"Length of longest possible route is "` ` ` `<< p.value; ` ` ` ` ` `// If the destination is not reachable ` ` ` `else` ` ` `cout << ` `"Destination not reachable from given source"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// input matrix with hurdles shown with number 0 ` ` ` `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 } ` ` ` `}; ` ` ` ` ` `// find longest path with source (0, 0) and ` ` ` `// destination (1, 7) ` ` ` `findLongestPath(mat, 0, 0, 1, 7); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Length of longest possible route is 24

This article is contributed by **Aditya Goel**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find shortest safe route in a path with landmines
- Check if it is possible to make the given matrix increasing matrix or not
- Maximum trace possible for any sub-matrix of the given matrix
- Find the longest path in a matrix with given constraints
- Longest Increasing Path in Matrix
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Check if possible to cross the matrix with given power
- Check for possible path in 2D matrix
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Check if possible to shuffle a matrix with adjacent movements
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Program to check diagonal matrix and scalar matrix
- Program to check if a matrix is Binary matrix or not