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 nn*mm 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 22 integers, n,mn,m(1n10001≤n≤1000,1m10001≤m≤1000)

Then follows nn lines with mm numbers aijaij

(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 InputSample 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

题解:挖金矿,如果map当前值是负,表示花费一定金币,如果为负,这个人就死了;问从1,1出发,最多可以得到多少金币;、

就是个dp题,我却各种dp预处理无限wa,其实就可以顺着思路,dp初始化为-1;dp[1][1]=map[1][1],如果当前大于等于0,就往下走,总共有四种姿势;

找最大的就好;

代码:

extern "C++"{
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1.0);
typedef long long LL;
typedef unsigned u;
typedef unsigned long long uLL;
void SI(int &x){scanf("%d",&x);}
void SI(LL &x){scanf("%lld",&x);}
void SI(u &x){scanf("%u",&x);}
void SI(uLL &x){scanf("%llu",&x);}
void SI(double &x){scanf("%lf",&x);}
void SI(char *x){scanf("%s",&x);}

void PI(int &x){printf("%d",x);}
void PI(LL &x){printf("%lld",x);}
void PI(u &x){printf("%u",x);}
void PI(uLL &x){printf("%llu",x);}
void PI(double &x){printf("%lf",x);}
void PI(char *x){printf("%s",x);}
#define mem(x,y) memset(x,y,sizeof(x))
#define NL puts("");
}
const int MAXN = 1010;
int n,m;
int a[MAXN][MAXN];
int dp[MAXN][MAXN];
int ans;
int disx[4] = {1,0,1,2};
int disy[4] = {0,1,2,1};
int main(){
	while(~scanf("%d%d",&n,&m)){
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				scanf("%d",&a[i][j]);
			}
		}
		memset(dp,-1,sizeof(dp));
		dp[1][1] = a[1][1];
		if(a[1][1] < 0){
			puts("0");
			continue;
		}
		ans = a[1][1];
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				if(dp[i][j] >= 0){
					for(int k = 0;k < 4;k++){
						int nx = i + disx[k];
						int ny = j + disy[k];
						dp[nx][ny] = max(dp[nx][ny],a[nx][ny] + dp[i][j]);
						ans = max(ans,dp[nx][ny]);
					}
				}
			}
		}
		printf("%d
",ans);
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/handsomecui/p/5315270.html