UESTC 1271 Search gold (DP)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1271


题意:给你一个地图,起点在(1,1),求能得到的珠宝最多是多少

思路:因为只能朝着那几个方向走,所以说就是简单的DP问题了,刚做的时候没看这道题,也没做,唉= =


ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define mod 1000000007
using namespace std;
int n,m;
int dp[1010][1010];
int map[1010][1010];
int main()
{
	int i,j,k;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=2;i<=n+1;i++)
		{
			for(j=2;j<=m+1;j++)
			{
				scanf("%d",&map[i][j]);
			}
		}
		for(i=0;i<=n+1;i++)
		{
			for(j=0;j<=m+1;j++)
			{
				dp[i][j]=-INF;
			}
		}
		int ans=map[2][2];
		for(i=2;i<=n+1;i++)
		{
			for(j=2;j<=m+1;j++)
			{
				if(i==2&&j==2)
				{
					dp[2][2]=map[i][j];
					continue;
				}
				dp[i][j]=max(max(dp[i-1][j]+map[i][j],dp[i][j-1]+map[i][j]),max(dp[i-1][j-2]+map[i][j],dp[i-2][j-1]+map[i][j]));
				if(dp[i][j]<=0)
				dp[i][j]=-INF;
				ans=max(dp[i][j],ans);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(UESTC 1271 Search gold (DP))