Java实现 洛谷 P1508 Likecloud-吃、吃、吃_java

Java实现 洛谷 P1508 Likecloud-吃、吃、吃_JAVA_02


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])); // 因为最大值只可能在李大水牛的前方、左前方、右前方,所以只要找这三个的最大就行了
	}

}

Java实现 洛谷 P1508 Likecloud-吃、吃、吃_java

Java实现 洛谷 P1508 Likecloud-吃、吃、吃_JAVA_02


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])); // 因为最大值只可能在李大水牛的前方、左前方、右前方,所以只要找这三个的最大就行了
	}

}