Skip to content

Commit 5d76e35

Browse files
committed
Longest Path in Matrix
1 parent dac538b commit 5d76e35

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// C++ program to find the longest path in a matrix
2+
// with given constraints
3+
#include <bits/stdc++.h>
4+
#define n 3
5+
using namespace std;
6+
7+
// Returns length of the longest path beginning with mat[i][j].
8+
// This function mainly uses lookup table dp[n][n]
9+
int findLongestFromACell(int i, int j, int mat[n][n], int dp[n][n])
10+
{
11+
if (i < 0 || i >= n || j < 0 || j >= n)
12+
return 0;
13+
14+
// If this subproblem is already solved
15+
if (dp[i][j] != -1)
16+
return dp[i][j];
17+
18+
// To store the path lengths in all the four directions
19+
int x = INT_MIN, y = INT_MIN, z = INT_MIN, w = INT_MIN;
20+
21+
// Since all numbers are unique and in range from 1 to n*n,
22+
// there is atmost one possible direction from any cell
23+
if (j < n - 1 && ((mat[i][j] + 1) == mat[i][j + 1]))
24+
x = 1 + findLongestFromACell(i, j + 1, mat, dp);
25+
26+
if (j > 0 && (mat[i][j] + 1 == mat[i][j - 1]))
27+
y = 1 + findLongestFromACell(i, j - 1, mat, dp);
28+
29+
if (i > 0 && (mat[i][j] + 1 == mat[i - 1][j]))
30+
z = 1 + findLongestFromACell(i - 1, j, mat, dp);
31+
32+
if (i < n - 1 && (mat[i][j] + 1 == mat[i + 1][j]))
33+
w = 1 + findLongestFromACell(i + 1, j, mat, dp);
34+
35+
// If none of the adjacent fours is one greater we will take 1
36+
// otherwise we will pick maximum from all the four directions
37+
return dp[i][j] = max(x, max(y, max(z, max(w, 1))));
38+
}
39+
40+
// Returns length of the longest path beginning with any cell
41+
int finLongestOverAll(int mat[n][n])
42+
{
43+
int result = 1; // Initialize result
44+
45+
// Create a lookup table and fill all entries in it as -1
46+
int dp[n][n];
47+
memset(dp, -1, sizeof dp);
48+
49+
// Compute longest path beginning from all cells
50+
for (int i = 0; i < n; i++) {
51+
for (int j = 0; j < n; j++) {
52+
if (dp[i][j] == -1)
53+
findLongestFromACell(i, j, mat, dp);
54+
55+
// Update result if needed
56+
result = max(result, dp[i][j]);
57+
}
58+
}
59+
60+
return result;
61+
}
62+
63+
// Driver program
64+
int main()
65+
{
66+
int mat[n][n] = { { 1, 2, 9 },
67+
{ 5, 3, 8 },
68+
{ 4, 6, 7 } };
69+
cout << "Length of the longest path is "
70+
<< finLongestOverAll(mat);
71+
return 0;
72+
}

Dynamic_Programming/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,22 @@
114114
Output: 4
115115
Explanation: Total length is 4, and the cut lengths are 2, 1 and 1. We can make maximum 4 segments each of length 1.
116116

117+
## 15. Longest Path in Matrix:
118+
Given a n*n matrix where all numbers are distinct, find the maximum length path (starting from any cell) such that all cells along the path are in increasing order with a difference of 1.
119+
We can move in 4 directions from a given cell (i, j), i.e., we can move to (i+1, j) or (i, j+1) or (i-1, j) or (i, j-1) with the condition that the adjacent cells have a difference of 1.
120+
Example 1:
121+
Input: mat[][] = {{1, 2, 9}
122+
{5, 3, 8}
123+
{4, 6, 7}}
124+
Output: 4
125+
The longest path is 6-7-8-9.
117126
## 16. Minimum Sum Partition:
118127
Given an integer array arr of size N, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum and find the minimum difference
119128
Example 1:
120129
Input: N = 4, arr[] = {1, 6, 11, 5}
121130
Output: 1
122131
Explanation: Subset1 = {1, 5, 6}, sum of Subset1 = 12 Subset2 = {11}, sum of Subset2 = 11
132+
123133
## 17. Count Number of Hops:
124134
A frog jumps either 1, 2, or 3 steps to go to the top. In how many ways can it reach the top. As the answer will be large find the answer modulo 1000000007.
125135
Example 1:

0 commit comments

Comments
 (0)