AcWing1015摘花生问题dpJava版

AcWing1015摘花生问题dpJava版_第1张图片

AcWing1015摘花生问题dpJava版_第2张图片

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
	static int N = 110;
	static int[][] dp = new int[N][N];  ;//存放的是dp[1][1]~dp[R][C]的最大花生数量
                                    //dp[i][j] 由max(dp[i - 1][j],dp[i][j - 1])更新而来
	    
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) throws IOException{
		int T = Integer.parseInt(in.readLine());
		while(T -- > 0) {
			String[] init = in.readLine().split(" ");
			int x = Integer.parseInt(init[0]);
			int y = Integer.parseInt(init[1]);
			for(int i = 1;i <= x;i ++) {
				init = in.readLine().split(" ");
				for(int j = 1;j <= y;j ++) {
					dp[i][j] = Integer.parseInt(init[j - 1]); 
					dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + dp[i][j];
				}
			}
			System.out.println(dp[x][y]);
		}
		in.close();
	}
	
}

你可能感兴趣的:(算法,算法,java)