AcWing 1015. 摘花生
原题链接
简单
作者:
不知名的fE
,
2024-11-20 19:02:07
,
所有人可见
,
阅读 1
import java.util.*;
public class Main {
static final int N = 110;
static int[][] g = new int[N][N];
static int T, n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
T = Integer.parseInt(sc.nextLine());
while (T -- > 0) {
String[] s = sc.nextLine().split(" ");
n = Integer.parseInt(s[0]); m = Integer.parseInt(s[1]);
for (int i = 1; i <= n; i++) g[i] = toArr(sc.nextLine().split(" "));
int ans = dp();
System.out.println(ans);
}
}
static int dp() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
//对于g[i][j] 有两条路可以走到改地方,
//从上面走过来或者从左边走过来,取大值累加即可
g[i][j] += Math.max(g[i - 1][j], g[i][j - 1]);
return g[n][m];
}
static int[] toArr(String[] s) {
int[] res = new int[N];
for (int i = 1; i <= s.length; i++) res[i] = Integer.parseInt(s[i - 1]);
return res;
}
}