Java实现 洛谷 P1508 Likecloud-吃、吃、吃
转载
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int n, m;
static int[][] a;
static int[][] f;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
f = new int[201][201];
a = new int[201][201];
for (int i = 0; i < 201; i++)
Arrays.fill(a[i], -100000);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = sc.nextInt();
}
}
int y = m / 2 + 1, x = n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = Math.max(Math.max(f[i - 1][j], f[i - 1][j - 1]), f[i - 1][j + 1]) + a[i][j]; // 动态方程
}
}
System.out.println(Math.max(Math.max(f[x][y], f[x][y - 1]), f[x][y + 1])); // 因为最大值只可能在李大水牛的前方、左前方、右前方,所以只要找这三个的最大就行了
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int n, m;
static int[][] a;
static int[][] f;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
f = new int[201][201];
a = new int[201][201];
for (int i = 0; i < 201; i++)
Arrays.fill(a[i], -100000);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = sc.nextInt();
}
}
int y = m / 2 + 1, x = n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = Math.max(Math.max(f[i - 1][j], f[i - 1][j - 1]), f[i - 1][j + 1]) + a[i][j]; // 动态方程
}
}
System.out.println(Math.max(Math.max(f[x][y], f[x][y - 1]), f[x][y + 1])); // 因为最大值只可能在李大水牛的前方、左前方、右前方,所以只要找这三个的最大就行了
}
}
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。