UESTC 1271 Search gold【dp】

Search gold

Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu

Description

Dreams of finding lost treasure almost came true recently. A new machine called 'The Revealer' has been invented and it has been used to detect gold which has been buried in the ground. The machine was used in a cave near the seashore where – it is said – pirates used to hide gold. The pirates would often bury gold in the cave and then fail to collect it. Armed with the new machine, a search party went into the cave hoping to find buried treasure. The leader of the party was examining the soil near the entrance to the cave when the machine showed that there was gold under the ground. Very excited, the party dug a hole two feel deep. They finally found a small gold coin which was almost worthless. The party then searched the whole cave thoroughly but did not find anything except an empty tin trunk. In spite of this, many people are confident that 'The Revealer' may reveal something of value fairly soon.

So,now you are in the point (1,1) (1,1) and initially you have 0 gold.In the  n n* m m grid there are some traps and you will lose gold.If your gold is not enough you will be die.And there are some treasure and you will get gold.If you are in the point(x,y),you can only walk to point  (x+1,y),(x,y+1),(x+1,y+2) (x+1,y),(x,y+1),(x+1,y+2)and (x+2,y+1) (x+2,y+1).Of course you can not walk out of the grid.Tell me how many gold you can get most in the trip.

It`s guarantee that (1,1) (1,1)is not a trap;

Input

first come  2 2 integers,  n,m n,m( 1n1000 1≤n≤1000, 1m1000 1≤m≤1000)

Then follows  n n lines with  m m numbers  aij aij

(100<=aij<=100) (−100<=aij<=100)

the number in the grid means the gold you will get or lose.

Output

print how many gold you can get most.

Sample Input

3 3 
1 1 1 
1 -5 1 
1 1 1


3 3 
1 -100 -100 
-100 -100 -100 
-100 -100 -100

Sample Output

5


1

题意:

有一张地图,每个位置都对应一个数字,表示得到或者损失的金币数量,有个人可以按题目描述的方式进行走动,并且某一步金币小于零的话,人就不能再行走,问这个人最多能获得多少个金币?(不管在任何地方,只需要统计曾今拥有的金币的最大值就行)


题解:


如果不是数据太大了,暴力搜索也是可以过的,但是这么大的数据量只能想到用dp 了,可怜我dp,连入门都没有..........

大约照着感觉推导了一下,结果样例过了,很惊喜的去提交,WA了.....

后来才想到自己那样无脑的dp 刷表,忘了考虑那个人死在半路上的情况了,果断修改,顺利AC(个人觉得后台数据貌似有点弱...)


动态更新过程:

走到每一步的时候,比较所有能到达当前位置的其他位置上的数(注意判断死在半路上的),找到最大的哪个值,刷表....


/*
http://blog.csdn.net/liuke19950717
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,x[1005][1005],dp[1005][1005];
int dx[]={0,1,1,2},dy[]={1,0,2,1};
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		memset(dp,0,sizeof(dp));
		for(int i=2;i<=n+1;++i)//为处理方便,移动了一下整个图
		{
			for(int j=2;j<=m+1;++j)
			{
				scanf("%d",&x[i][j]);
			}
		}
		int ans=dp[2][2];
		dp[2][2]=x[2][2];//起始位置
		for(int i=2;i<=n+1;++i)//行
		{
			for(int j=2;j<=m+1;++j)//列
			{
				for(int k=0;k<4;++k)//遍历能到达这个位置的其他四个位置
				{
					int tx=i-dx[k],ty=j-dy[k];
					if(dp[tx][ty]>0)//没有死在路上才能进行尝试选择
					{
						dp[i][j]=max(dp[tx][ty]+x[i][j],dp[i][j]);
					
					}	
				}
				ans=max(ans,dp[i][j]);//更新出现的最大值
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值