-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLongest_Path.cpp
71 lines (57 loc) · 1.99 KB
/
Longest_Path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// This C++ code gives longest path in a matrix of size (row*coloumn) containing numbers from 1 to row*coloumn...
#include<bits/stdc++.h>
using namespace std;
#define row 3
#define coloumn 3
// Function to return length of the longest path beginning with mat[i][j]...
int findLongestFromACell(int i, int j, int mat[row][coloumn], int dp[row][coloumn])
{
// Base case
if (i<0 || i>=row || j<0 || j>=coloumn)
return 0;
// If this subproblem is already solved
if (dp[i][j] != -1)
return dp[i][j];
// Since all numbers are unique and in range from 1 to n*n,
// there is atmost one possible direction from any cell
if (j<coloumn-1 && ((mat[i][j] +1) == mat[i][j+1]))
return dp[i][j] = 1 + findLongestFromACell(i,j+1,mat,dp);
if (j>0 && (mat[i][j] +1 == mat[i][j-1]))
return dp[i][j] = 1 + findLongestFromACell(i,j-1,mat,dp);
if (i>0 && (mat[i][j] +1 == mat[i-1][j]))
return dp[i][j] = 1 + findLongestFromACell(i-1,j,mat,dp);
if (i<row-1 && (mat[i][j] +1 == mat[i+1][j]))
return dp[i][j] = 1 + findLongestFromACell(i+1,j,mat,dp);
// If none of the adjacent fours is one greater
return dp[i][j] = 1;
}
// Returns length of the longest path beginning with any cell
int finLongestOverAll( int matrix[row][coloumn] )
{
int answer = 1;
// Create a lookup table and fill all entries in it as -1
int dp[row][coloumn];
memset(dp, -1, sizeof dp);
// Compute longest path beginning from all cells
for (int i=0; i<row; i++)
{
for (int j=0; j<coloumn; j++)
{
if (dp[i][j] == -1)
findLongestFromACell(i, j, matrix, dp);
// Update result if needed
answer = max(answer, dp[i][j]);
}
}
return answer;
}
// Driver program...
int main()
{
int matrix[row][coloumn];
for(int i=0;i<row;i++)
for(int j=0;j<coloumn;j++)
cin >> matrix[i][j];
cout << "Length of the longest path is :"<< finLongestOverAll(matrix);
return 0;
}