-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIAMONDPATH.cpp
68 lines (59 loc) · 1.44 KB
/
DIAMONDPATH.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
#include <iostream>
#include <algorithm>
using namespace std;
int diamond[200][200];
int lookup[200][200];
void solve(int nMid);
int getUpperMax(int i, int j);
int getLowerMax(int i, int j);
int main()
{
int nTest;
cin >> nTest;
while (nTest--)
{
int nMid;
cin >> nMid;
for (int i = 0; i < nMid; i++)
{
for (int j = 0; j < i + 1; j++)
cin >> diamond[i][j];
}
int nLine = 2 * nMid - 1;
for (int i = nMid; i < nLine; i++)
{
for (int j = 0; j < nLine - i; j++)
cin >> diamond[i][j];
}
solve(nMid);
cout << lookup[nLine - 1][0] << endl;
}
}
void solve(int nMid)
{
int nLine = 2 * nMid - 1;
for (int i = 0; i < nLine; i++)
{
if (i < nMid)
{
for (int j = 0; j < i + 1; j++)
lookup[i][j] = diamond[i][j] + getUpperMax(i, j);
}
else
{
for (int j = 0; j < nLine - i; j++)
lookup[i][j] = diamond[i][j] + getLowerMax(i, j);
}
}
}
int getUpperMax(int i, int j)
{
if (i == 0) return 0;
if (j == 0) return lookup[i - 1][j];
if (j == i) return lookup[i - 1][j - 1];
return max(lookup[i - 1][j - 1], lookup[i - 1][j]);
}
int getLowerMax(int i, int j)
{
return max(lookup[i - 1][j], lookup[i - 1][j + 1]);
}