

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the longest path in a matrix with given constraints in C++
Suppose we have one square matrix of order n. It has all distinct elements. So we have to find the maximum length path, such that all cells along the path are in increasing order with a difference of 1. From one cell we can move to four directions. Left, Right, Top and Bottom. So if the matrix is like −
1 | 2 | 9 |
5 | 3 | 8 |
4 | 6 | 7 |
So the output will be 4. As the longest path is 6→7→8→ 9
To solve this problem, we will follow this idea. We will calculate longest path beginning with every cell. Once we have got the longest for all cells, we return maximum of all longest paths.
One important observation in this approach is many overlapping sub-problems. So this problem can be solved using Dynamic Programming. Here we will use a lookup table dp[][] to check if a problem is already solved or not.
Example
#include <iostream> #define n 3 using namespace std; int getLongestPathLengthUtil(int i, int j, int matrix[n][n], int table[n][n]) { if (i < 0 || i >= n || j < 0 || j >= n) return 0; if (table[i][j] != -1) return table[i][j]; int x = INT_MIN, y = INT_MIN, z = INT_MIN, w = INT_MIN; if (j < n - 1 && ((matrix[i][j] + 1) == matrix[i][j + 1])) x = 1 + getLongestPathLengthUtil(i, j + 1, matrix, table); if (j > 0 && (matrix[i][j] + 1 == matrix[i][j - 1])) y = 1 + getLongestPathLengthUtil(i, j - 1, matrix, table); if (i > 0 && (matrix[i][j] + 1 == matrix[i - 1][j])) z = 1 + getLongestPathLengthUtil(i - 1, j, matrix, table); if (i < n - 1 && (matrix[i][j] + 1 == matrix[i + 1][j])) w = 1 + getLongestPathLengthUtil(i + 1, j, matrix, table); return table[i][j] = max(x, max(y, max(z, max(w, 1)))); } int getLongestPathLength(int matrix[n][n]) { int result = 1; int table[n][n]; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) table[i][j] = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (table[i][j] == -1) getLongestPathLengthUtil(i, j, matrix, table); result = max(result, table[i][j]); } } return result; } int main() { int mat[n][n] = { { 1, 2, 9 }, { 5, 3, 8 }, { 4, 6, 7 } }; cout << "Length of the longest path is "<< getLongestPathLength(mat); }
Output
Length of the longest path is 4
- Related Questions & Answers
- C++ program to find last value of matrix with given constraints
- Longest Increasing Path in a Matrix in Python
- Find length of the longest consecutive path from a given starting characters in C++
- Program to length of longest increasing path in a given matrix in Python
- Find duplicates under given constraints in C++
- Maximum number of ones in a N*N matrix with given constraints in C++
- Program to find length of longest matrix path length in Python
- Find sub-matrix with the given sum in C++
- Find the Pair with Given Sum in a Matrix using C++
- Longest consecutive path from a given starting character
- Find minimum time to finish all jobs with given constraints in C++
- Add the elements of given arrays with given constraints?
- Longest ZigZag Path in a Binary Tree in C++
- Program to find length of longest path with even sum in Python
- Add elements of given arrays with given constraints?