UESTC--1271--Search gold(dp)(坑)

Search gold

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

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 and output

Sample Input Sample Output
3 3
1 1 1
1 -5 1
1 1 1
5
3 3
1 -100 -100
-100 -100 -100
-100 -100 -100
1

Source

第七届ACM趣味程序设计竞赛第四场(正式赛)


0.0这道题真的是做过啊,但是wa了9次!!!!!dfs还有bfs都试了,就是不对啊,第一组数据到底是个什么鬼!!!以后再也不随便搜索了

因为x方向一致是正向移动,y轴也是,所以贪心吧

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[1010][1010];
int dp[1010][1010];
int dx[4]={1,0,1,2};
int dy[4]={0,1,2,1};
int main()
{
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		memset(dp,-1,sizeof(dp));
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		scanf("%d",&map[i][j]);
		int ans=-1;
		dp[0][0]=map[0][0];
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(dp[i][j]<0)
				continue;//如果现在的状态小于零,不考虑 
				ans=max(ans,dp[i][j]);//这个状态是已经改变过的状态 
				for(int k=0;k<4;k++)//列举出这个点所有可以到达的点 
				{
					int x=i+dx[k];
					int y=j+dy[k];
					if(x<0||x>=n||y<0||y>=m)
					continue;//不能越界 
					dp[x][y]=max(dp[i][j]+map[x][y],dp[x][y]);//是否有必要走到这里 
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值