
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Longest Increasing Path in a Matrix in Python
- Program to length of longest increasing path in a given matrix in Python
- C++ program to find last value of matrix with given constraints
- Program to find length of longest matrix path length in Python
- Maximum number of ones in a N*N matrix with given constraints in C++
- Find length of the longest consecutive path from a given starting characters in C++
- Longest consecutive path from a given starting character
- Find duplicates under given constraints in C++
- Program to find length of longest path with even sum in Python
- Find minimum time to finish all jobs with given constraints in Python
- Find minimum time to finish all jobs with given constraints in C++
- Add the elements of given arrays with given constraints?
- Find the longest substring with k unique characters in a given string in Python
- Find the Pair with Given Sum in a Matrix using C++
- Find sub-matrix with the given sum in C++
