zzulioj 1875

题目链接:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1875

题目大意:一个n*m矩形上分布着价值不等的财宝,两个人从【1,1】点只能朝下或右走,经过一点就将该点财宝拾起,该点不再有财宝。问他俩从【n,m】点离开时最多获得多少价值的财宝?

分析:刚开始会很容易想到分别对两个人dp,接着求和获得答案。但这是不对的,因为两个人并非完全没有关系,所以必须将他们一起讨论。我们设dp[step][r1][r2]:两人走了step步,纵坐标分别位于r1,r2处,建立在两者接下来的走法完全正确时所能获得的价值。这样定义是当两者都位于【n,m】点这个状态的价值就是这点的价值,是已经知道的。可由这个点推出全部的状态。我的解法就是这样了,当然大哥哥大姐姐们要是有另外的解法一定要私信我~
下面是代码:

#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 110;
int mapp[N][N],dp[N<<1][N][N],r,c;
int dfs(int step,int r1,int r2);
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int sum = 0;
        cin>>r>>c;
        for(int i=1; i<=r; i++)
          for(int j=1; j<=c; j++)
          {
              cin>>mapp[i][j]; sum += mapp[i][j];
          }

        memset(dp,-1,sizeof(dp));
        if(r==1 || c==1)
            cout<<sum<<endl;
        else
            cout<<dfs(0,1,1)-mapp[1][1]<<endl;
    }
    return 0;
}
int dfs(int step,int r1,int r2)
{
    if(dp[step][r1][r2] != -1)
        return dp[step][r1][r2];
    int c1 = step - r1 + 2;
    int c2 = step - r2 + 2;
    /*走一样的点就不是最优了*/
    if(r1==r2 && c1==c2 && (r1!=r || c1!=c))
    {
        if(r1!=1 || c1!=1)//[1,1],[n,m]点不算。
           return 0;
    }
    int ans = 0;
    if(r1+1<=r && r2+1<=r)// 下下
        ans = max(ans,dfs(step+1,r1+1,r2+1));
    if(r1+1<=r && c2+1<=c) //下右
        ans = max(ans,dfs(step+1,r1+1,r2));
    if(c1+1<=c && r2+1<=r)// 右下
        ans = max(ans,dfs(step+1,r1,r2+1));
    if(c1+1<=c && c2+1<=c) //右右
        ans = max(ans,dfs(step+1,r1,r2));
    ans = ans + mapp[r1][c1] + mapp[r2][c2];
    if(r1==r && c2==c && r2==r && c2==c)
        ans -= mapp[r][c];
    //printf("%d %d  %d %d\n",r1,c1,r2,c2);
    return dp[step][r1][r2] = ans;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值