-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_sum_imp.cpp
72 lines (68 loc) · 1.36 KB
/
path_sum_imp.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
72
#include<bits/stdc++.h>
#define MAX 4
using namespace std;
void print(int a[MAX][MAX])
{
for(int i=0; i<MAX; i++)
{
for(int j=0; j<MAX; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
void print_ans(int mat[MAX][MAX], int len[MAX][MAX], int index)
{
int val = len[0][index] - mat[0][index];
int j = index;
for(int i=0; i<MAX-1; i++)
{
cout << mat[i][j] << " + ";
if(val == len[i+1][j-1])
j=j-1;
else if(val == len[i+1][j+1])
j = j+1;
val = val - mat[i+1][j];
if(val==0)
{
cout << mat[i+1][j] << endl;
break;
}
}
}
int solve(int mat[MAX][MAX])
{
int dp[MAX][MAX];
memset(dp, 0, sizeof(dp));
for(int i=MAX-1; i>=0; i--)
{
for(int j=MAX-1; j>=0; j--)
{
int l_d = (j==0||i==MAX-1)?0:dp[i+1][j-1];
int d = (i==MAX-1)?0:dp[i+1][j];
int r_d =(j==MAX-1||i==MAX-1)?0:dp[i+1][j+1];
dp[i][j] = mat[i][j] + max(l_d, max(d, r_d));
}
}
int res = dp[0][0], index=0;
for(int j=1; j<MAX; j++)
{
res = max(res, dp[0][j]);
if(res == dp[0][j])
index = j;
}
//cout << "index: " << index << endl;
//cout << "MATRIX:" << endl;
//print(dp);
//cout << endl;
print_ans(mat, dp, index);
return res;
}
int main()
{
int mat[4][4] = {{4, 2, 3, 4},
{2, 9, 10, 10},
{15, 1, 3, 10},
{16, 92, 41, 104}};
cout << solve(mat);
return 0;
}