Search gold (DP)


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)$ and initially you have 0 gold.In the $n$*$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)$and$(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)$is not a trap;

Input

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

Then follows $n$ lines with $m$ numbers $ a_{ij} $

$(-100<=a_{ij}<=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

注意边界即可,一开始以为要搜,但是发现n的数据太大。



#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1111][1111],dp[1111][1111];
const int inf = 0x3f3f3f3f;
int main()
{
	int n,m,i,j;
	while(cin>>n>>m) {
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++) {
			for(j=1;j<=m;j++)
			cin>>a[i][j];
		}
		for(i=1;i<=n;i++) {
			dp[i][1]=dp[i-1][1]+a[i][1];
			if(dp[i][1]<=0) dp[i][1]=-inf;
		}
		for(j=1;j<=m;j++) {
			dp[1][j]=dp[1][j-1]+a[1][j];
			if(dp[1][j]<=0) dp[1][j]=-inf;
		}
		for(i=2;i<=n;i++) {
			for(j=2;j<=m;j++) {
				int x=i,y=j;
				if(x-2>0 && y-2>0) {
					dp[i][j]=max(max(dp[x-1][y],dp[x][y-1]),max(dp[x-1][y-2],dp[x-2][y-1]))+a[i][j];
				}
				else if(x-2>0) {
					dp[i][j]=max(dp[x-2][y-1],max(dp[x-1][y],dp[x][y-1]))+a[i][j];
				}
				else if(y-2>0) {
					dp[i][j]=max(dp[x-1][y-2],max(dp[x-1][y],dp[x][y-1]))+a[i][j];
				}
				else dp[i][j]=max(dp[x-1][y],dp[x][y-1])+a[i][j];
				if(dp[i][j]<0) dp[i][j]=-inf;
			}
			if(dp[i][j]<0) dp[i][j]=-inf;
		}
		int ans=a[1][1];
		for(i=1;i<=n;i++) {
			for(j=1;j<=m;j++)
			ans=max(ans,dp[i][j]);
		}
		cout<<ans<<endl;
	}
	return 0;
}










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值