题意理解
这个和数字金字塔真的是一样的,就是拓展的方向多了一个。。。
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int M, N;
static final int maxn = 220;
static int[][] a = new int[maxn][maxn];
static int[][] dp = new int[maxn][maxn];
public static void main(String[] args) {
FastScanner fs = new FastScanner();
M = fs.nextInt();
N = fs.nextInt();
for(int i = 1; i <= M; i++) {
for(int j = 1; j <= N; j++) {
a[i][j] = fs.nextInt();
}
}
for(int i = 1; i <= M; i++) {
for(int j = 1; j <= N; j++) {
dp[i][j] = Math.max(Math.max(dp[i-1][j-1], dp[i-1][j]), dp[i-1][j+1]) + a[i][j];
}
}
int x = M;
int y = N / 2 + 1;
int max = Math.max(Math.max(dp[x][y], dp[x][y-1]), dp[x][y+1]);
System.out.println(max);
}
public static class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String nextToken() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.valueOf(nextToken());
}
}
}