UESTC271(dp)
来源:互联网 发布:htpp js.ynjy.cn 编辑:程序博客网 时间:2024/05/21 06:46
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
It`s guarantee that
Input
first come
Then follows
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
3 31 1 11 -5 11 1 1
5
3 31 -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 0x3f3f3fint 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 0x3f3f3fint 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; }
- UESTC271(dp)
- colors颜色的长度 (来自刘汝佳紫书) dp+dp+dp
- 区间DP -- 石子合并(朴素DP)
- 疯狂 dp(一) 线性 dp
- hdu 3480 Division(DP-DP优化)
- hdu 2196 Computer(DP-树形DP)
- hdu 4044 GeoDefense(DP-树形DP)
- HDOJ-4276(树形DP+背包DP)
- DP·数位DP(2)
- DP·数位DP(4)
- DP·数位DP(5)
- HDU 4001--Dp--(第一道dp)
- bzoj1801(dp棋盘上方案dp)
- POJ2411(状压Dp,插头Dp,数学公式)
- smoj1710:砖块II (dp套dp)
- hdu4336(壮压DP&概率DP)
- HDOJ2571 DP入门题(DP+DFS)
- POJ2796Feel Good(DP)
- Codeforces #305 div2 E. Mike and Foam 数论 容斥原理
- [小技巧]导文件超出php或nginx超时时间,怎们办?
- JavaScript 中文字符占两个字节处理方法
- 算法总结(第二篇)
- Deep Learning(3.21)
- UESTC271(dp)
- 从头认识设计模式-策略模式-07-对策略模式的总结
- Ubuntu系统 VI 编辑器初试
- go语言在linux平台上的搭建
- 使用scm-manager搭建git/svn 代码管理仓库
- Java之 redirect 和 forward【整理】
- Android关于log日志,华为不输出log.v,log.d
- HDU 2159 FATE ,完全背包
- 关于linux系统端口查看和占用的解决方案