普及练习场 多维动态规划 Likecloud-吃、吃、吃

题目链接

题意理解

这个和数字金字塔真的是一样的,就是拓展的方向多了一个。。。

代码

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());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值