-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
60 lines (46 loc) · 1.49 KB
/
main.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
#include <bits/stdc++.h>
int get_idx(const int n, const int i1, const int j1, const int vd) {
return n * (n * i1 + j1) + vd;
}
// go with two agents to (n-1,n-1) simultaneously
int rec(
std::vector<int> &mem,
const std::vector<std::vector<int>> &as,
const int n,
const int i1,
const int j1,
const int vd
) {
const int i2 = i1 - vd;
const int j2 = j1 + vd;
if (i1 < 0 || i1 >= n || i2 < 0 || i2 >= n || j1 < 0 || j1 >= n || j2 < 0 || j2 >= n) return 0;
if (i1 == n - 1 && j1 == n - 1 && i2 == n - 1 && j2 == n - 1) return as[n - 1][n - 1];
const int idx = get_idx(n, i1, j1, vd);
if (mem[idx] != -1) return mem[idx];
int result = (i1 == i2 && j1 == j2) ? as[i1][j1] : as[i1][j1] + as[i2][j2];
const int a = rec(mem, as, n, i1 + 1, j1, vd); // down, down
const int b = rec(mem, as, n, i1 + 1, j1, vd + 1); // down, right
const int c = rec(mem, as, n, i1, j1 + 1, vd - 1); // right, down
const int d = rec(mem, as, n, i1, j1 + 1, vd); // right, right
result += std::max({a, b, c, d});
mem[idx] = result;
return result;
}
void solve() {
int n; std::cin >> n;
std::vector<std::vector<int>> as(n, std::vector<int>());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int a; std::cin >> a;
as[i].push_back(a);
}
}
std::vector<int> mem(n * n * n, -1);
int result = rec(mem, as, n, 0, 0, 0);
std::cout << result << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t; std::cin >> t;
while (t--) solve();
}