链接:https://ac.nowcoder.com/acm/contest/984/K
来源:牛客网

金币馅饼
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
最近,奶牛们热衷于把金币包在面粉里,然后把它们烤成馅饼。第i块馅饼中含有Ni(1<=Ni<=25)块金币,并且,这个数字被醒目地标记在馅饼表面。
奶牛们把所有烤好的馅饼在草地上排成了一个R行(1<=R<=100)C列(1<=C<=100)的矩阵。你现在站在坐标为(1,1)的馅饼边上,当然,你可以拿到那块馅饼里的所有金币。你必须从现在的位置,走到草地的另一边,在坐标为(R,C)的馅饼旁边停止走动。每做一次移动,你必须走到下一列的某块馅饼旁边,并且,行数的变动不能超过1(也就是说,如果现在你站在坐标为(r,c)的馅饼边上,下一步你可以走到坐标为(r-1,c+1),(r,c+1),或者(r+1,c+1)的馅饼旁边)。当你从一块馅饼边经过,你就可以拿走馅饼里所有的金币。当然啦,你一定不会愿意因半路离开草地而失去唾手可得的金币,但,最终你一定得停在坐标为(R,C)的馅饼旁边。
现在,你拿到了一张标记着馅饼矩阵中,每一块馅饼含金币数量的表格。那么,按照规则,你最多可以拿到多少金币呢?
比方说,奶牛们把馅饼排成如下的矩阵,矩阵中的数字表示该位置的馅饼中含金币的数量:

起点-> 6 5 3 7 9 2 7
2 4 3 5 6 8 6
4 9 9 9 1 5 8 <-终点
以下是一条合法的路线:

起点-> 6 5 3 7 9 2 7

2 4 3 5 6 8 6
\ /
4 9 9-9 1 5-8 <-终点
按上述的路线进行走动,一共可以获得6+4+9+9+6+5+8=47个金币。按照规则,在这个矩阵中最多可以得到50个金币,路线如下图所示:

起点-> 6 5 3 7 9 2 7

2 4 3 5 6-8 6
\ /
4 9 9-9 1 5 8 <-终点
(请复制到记事本中用等宽字体查看)

输入描述:
第1行: 两个用空格隔开的整数,R和C
第2..R+1行: 每行包含C个用空格隔开的正整数,依次表示一行中从左往右各个馅饼里金币的数量
输出描述:
第1行: 输出一个正整数,表示你所能收集到的最大金币数目
示例1
输入
复制
3 7
6 5 3 7 9 2 7
2 4 3 5 6 8 6
4 9 9 9 1 5 8
输出
复制
50

思路:
直接二维DP 嘛,每一个位置可能由最多3个位置转移过来,还需要注意一下有些位置是没法访问到的,比如第一列,只有1,1这个位置可以访问,其他均无法到达,转移的时候需要判断一下。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int dp[105][105];
int a[105][105];
int n,m;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin>>n>>m;
    repd(i,1,n)
    {
        repd(j,1,m)
        {
            cin>>a[i][j];
        }
    }
    memset(dp,-1,sizeof(dp));
    dp[1][1]=a[1][1];
    repd(j,2,m)
    {
        repd(i,1,n)
        {
            if(dp[i][j-1]!=-1)
                dp[i][j]=max(dp[i][j],dp[i][j-1]+a[i][j]);
            if(dp[i+1][j-1]!=-1)
                dp[i][j]=max(dp[i][j],dp[i+1][j-1]+a[i][j]);
            if(dp[i-1][j-1]!=-1)
                dp[i][j]=max(dp[i][j],dp[i-1][j-1]+a[i][j]);
        }
    }
    dp[n][m]=max(dp[n][m],0);
    cout<<dp[n][m]<<endl;
    return 0;
}
 
inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}