题意: 给一个 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 还可以这样玩 》=《。