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( 1≤n≤1000 1≤n≤1000, 1≤m≤1000 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 |
//UESTC1271(dp)
//这题用搜索估计会超时,所以只能用dp,刚开始方向搞错,一直wa,注意更新的方向应为从现在的状态向前扩展,而不是
//向后,向后扩展一定会有重叠。
//动态转移方程为dp[i][j]=max(dp[i-1][j]+a[i][j],dp[i][j-1]+a[i][j],dp[i-1][j-2]+a[i][j],dp[i-2][j-1]+a[i][j]);
//其次就是边界,对于越界的点初始化为负无穷大,这样在更新就不会造成影响。
代码1:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int dp[1010][1010],a[1010][1010];
int main()
{
int n,m,i,j,k,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,-INF,sizeof(dp));
for(i=2;i<=n+1;i++)
{
for(j=2;j<=m+1;j++)
{
scanf("%d",&a[i][j]);
}
}
ans=dp[2][2]=a[2][2];
for(i=2;i<=n+1;i++)
{
for(j=2;j<=m+1;j++)
{
if(dp[i-1][j]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i-1][j]+a[i][j]);
if(dp[i][j-1]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i][j-1]+a[i][j]);
if(dp[i-1][j-2]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i-1][j-2]+a[i][j]);
if(dp[i-2][j-1]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i-2][j-1]+a[i][j]);
ans=max(ans,dp[i][j]);
}
}
printf("%d\n",ans);
}
return 0;
}
代码2:
<pre name="code" class="objc">//UESTC1271(dp)
//注意初始化,i==j的位置初始化为:dp[i][j]=a[i][j],其余dp[i][j]=-INF.
//因为此程序的行和列都是从1开始,所以要判断是否越界。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int dp[1010][1010],a[1010][1010];
int main()
{
int n,m,i,j,k,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(i==j) dp[i][j]=a[i][j];
else dp[i][j]=-INF;
}
}
ans=dp[1][1]=a[1][1];
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(i-1>=1&&dp[i-1][j]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i-1][j]+a[i][j]);
if(j-1>=1&&dp[i][j-1]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i][j-1]+a[i][j]);
if(i-1>=1&&j-2>=1&&dp[i-1][j-2]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i-1][j-2]+a[i][j]);
if(i-2>=1&&j-1>=1&&dp[i-2][j-1]+a[i][j]>=0)
dp[i][j]=max(dp[i][j],dp[i-2][j-1]+a[i][j]);
if(ans<dp[i][j]) ans=dp[i][j];
}
}
printf("%d\n",ans);
}
return 0;
}