You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
672 B

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int w[N][N];
int n, m;
int f[N][N];
int dfs(int x, int y) {
if (f[x][y]) return f[x][y];
if (x == n && y == m) return w[n][m];
if (x > n || y > m) return 0;
f[x + 1][y] = dfs(x + 1, y);
f[x][y + 1] = dfs(x, y + 1);
return f[x][y] = max(f[x + 1][y], f[x][y + 1]) + w[x][y];
}
int main() {
int T;
cin >> T;
while (T--) {
memset(f, 0, sizeof f);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> w[i][j];
printf("%d \n", dfs(1, 1));
}
return 0;
}