light oj 1071

题意: 给一个 r*c 的地图 , 每个点有自己的权值 , 两个人从 (1,1),点向(r,m)点前进, 求两人经过的醉的权值之和。
分析: 这道题不可以分别求两个路径 , 因为这样在决策时没有考虑到对另一个人的影响,我门校赛上有一道这样的题,我分开来写结果wa到结束。。 我们设 dp[step][i][j] 表示两人走了 step 步后分别在第i行和第j行上(列可以计算得出),这样我们便将他们两个联系了起来。
下面是代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
int dp[300][110][110],mapp[110][110],r,c;
int dfs(int step, int x, int x2);
int main()
{
    int t,i,j,cat = 0;
    freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&r,&c);
        for(i=1; i<=r; i++)
          for(j=1; j<=c; j++)
            scanf("%d",&mapp[i][j]);
        memset(dp,-1,sizeof(dp));

        printf("Case %d: %d\n",++cat,dfs(0,1,1)-mapp[1][1]); //权值只能给一个人
    }
    return 0;
}

int dfs(int step, int x, int x2)
{
    if(dp[step][x][x2] != -1)
        return dp[step][x][x2];

    int ay = step - x + 2;
    int by = step - x2 + 2;
    int ans = 0;
    if(ay+1<=c && by+1<=c && x!=x2) //右右
       ans = max(ans ,dfs(step+1,x,x2));
    if(x+1<=r && x2+1<=r && x!=x2) //下下
       ans = max(ans,dfs(step+1,x+1,x2+1));
    if(x+1<=r && by+1<=c && x+1 != x2) //下右
       ans = max(ans,dfs(step+1,x+1,x2));
    if(ay+1<=c && x2+1<=r && x != x2+1) //右上
       ans = max(ans,dfs(step+1,x,x2+1));

    if((x==r && ay==c-1 && x2==r-1 && by==c) || x2==r && by==c-1 && x2== r-1 && by==c) //如果下步必须是 (r,c)
        ans += mapp[r][c];

    /*将他们的落脚点的权值加上*/
    ans += mapp[x][ay] + mapp[x2][by];

    return dp[step][x][x2] = ans;
}

这是参考着学长的代码写的 , 原来 dp 还可以这样玩 》=《。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值