-
Notifications
You must be signed in to change notification settings - Fork 0
/
trianglepath.cpp
49 lines (36 loc) · 1006 Bytes
/
trianglepath.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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
vector<vector<int>> triangle;
int cache[100][100];
int solve(int row, int col) {
int& ret = cache[row][col];
if (ret != -1)
return cache[row][col];
if (row + 1 == triangle.size())
return ret = triangle[row][col];
ret = max(solve(row+1, col), solve(row+1, col+1)) + triangle[row][col];
return ret;
}
int main() {
int numTestCase;
cin >> numTestCase;
for (int i = 0; i < numTestCase; ++i) {
int sideLength;
cin >> sideLength;
triangle.clear();
triangle.assign(sideLength, vector<int>());
memset(cache, -1, sizeof(int) * 10000);
for (int j = 0; j < sideLength; ++j) {
for (int k = 0; k < j + 1; ++k) {
int num;
cin >> num;
triangle[j].push_back(num);
}
}
cout << solve(0, 0) << endl;
}
return 0;
}