-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwintergames.cpp
72 lines (53 loc) · 1.55 KB
/
wintergames.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 <iostream>
#include <algorithm>
using namespace std;
//recursive solution
int rpath(int *matrix, int *sum, int size, int x, int y){
if(x == 0 || y == 0 || y > x)
return 0;
if(sum[x + y*size] > 0)
return sum[x + y*size];
sum[x + y*size] = matrix[x + y*size] + max(rpath(matrix, sum, size, x-1, y-1), rpath(matrix, sum, size, x-1, y));
return sum[x + y*size];
}
//iterative solution
void ipath(int *matrix, int* sum, int size){
for(int x = 0; x < size; ++x){
for(int y = 0; y < x; ++y){
sum[x + y*size] = matrix[x + y*size] + max(sum[x-1 + (y-1)*size], sum[x-1 + y*size]);
}
}
}
int main(){
int cases;
cin >> cases;
for(int i = 0; i < cases; ++i){
int size;
cin >> size;
size++;
int *matrix = new int[size*size];
int *sum = new int[size*size];
for(int x = 0; x < size; ++x){
for(int y = 0; y < size; ++y){
sum[x + y*size] = 0;
if(x == 0 || y == 0 || x < y){
matrix[x + y*size] = 0;
continue;
}
cin >> matrix[x + y*size];
}
}
//recursive
for(int i = 0; i < size; ++i)
rpath(matrix, sum, size, size-1, i);
int max = 0;
for(int y = 0; y < size; ++y){
if(max < sum[(size-1) + y*size])
max = sum[(size-1) + y*size];
}
cout << max << "\n";
delete[] matrix;
delete[] sum;
}
return 0;
}