import java.util.Scanner;
public class Main_07_a{
/**
* 跳跃
* 动态规划 2.0
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入...
int n=scan.nextInt();
int m=scan.nextInt();
int arr[][]=new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j]= scan.nextInt();
}
}
scan.close();
//
int dp[][]=new int[n+3][m+3 ];
//dp初始化 dp[i][j]=-1000;
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
dp[i][j]=-1000;
}
}
skip(arr,dp);
}
private static void skip(int arr[][],int dp[][]){
int n=arr.length;
int m=arr[0].length;
dp[3][3]=arr[0][0];
for (int i = 3; i < dp.length; i++) {
for (int j = 3; j < dp[i].length; j++) {
if (!(i==3&&j==3)){
int max_arr[]=new int[9];
max_arr[0]=dp[i][j-3];
max_arr[1]=dp[i][j-2];
max_arr[2]=dp[i][j-1];
max_arr[3]=dp[i-1][j];
max_arr[4]=dp[i-1][j-1];
max_arr[5]=dp[i-1][j-2];
max_arr[6]=dp[i-2][j];
max_arr[7]=dp[i-2][j-1];
max_arr[8]=dp[i-3][j];
int max =max_arr[0];
for (int k = 1; k <=8; k++) {
if (max<max_arr[k]){
max=max_arr[k];
}
}
dp[i][j]=max+arr[i-3][j-3];
}
}
}
System.out.println(dp[n+2][m+2]);
}
}
【蓝桥杯】跳跃 Java
最新推荐文章于 2023-03-18 22:26:16 发布