nyoj 吃土豆 234 (双层DP)
2015-11-13 22:13
381 查看
吃土豆
时间限制:1000 ms | 内存限制:65535 KB难度:4
描述 Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want
to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas
are x-1 and x+1.
Now, how much qualities can you eat and then get ?
输入There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn't beyond 1000, and 1<=M,N<=500.
输出For each case, you just output the MAX qualities you can eat and then get.样例输入
4 6 11 0 7 5 13 9 78 4 81 6 22 4 1 40 9 34 16 10 11 22 0 33 39 6
样例输出
242
//题意: //给你n行m列的矩阵,矩阵上的数代表此处有多少豆子,问最多能吃多少豆子 //但吃豆子有一定的规则。 //规则::如果你吃了某处的豆子,那么你不能再吃它左边和右边的两处的豆子,并且不能吃它上下两行的豆子 //思路: //双层DP,先求出每行的最大值,再求全部的最大值。
#include<stdio.h> #include<string.h> int max(int x,int y) { return x>y?x:y; } int dp[510]; int a[510][510]; int main() { int n,m,i,j,w; while(scanf("%d%d",&n,&m)!=EOF) { memset(dp,0,sizeof(dp)); memset(a,0,sizeof(a)); for(i=3;i<n+3;i++) { for(j=3;j<m+3;j++) { scanf("%d",&w); a[i][j]=max(a[i][j-2],a[i][j-3])+w; } } for(i=3;i<n+3;i++) dp[i]=max(dp[i-2],dp[i-3])+max(a[i][m+1],a[i][m+2]); printf("%d\n",max(dp[n+1],dp[n+2])); } return 0; }
相关文章推荐
- 通过ipmitool监控机房内服务器温度
- 再读大道之简第七章第八章
- hdoj 2817 A sequence of numbers【快速幂】
- 自定义控件之继承ViewGroup(使用纯代码)
- 机器学习中的各种相似性、距离度量
- hdoj 2579 Dating with girls(2) 【bfs(标记状态)】
- 不知所终的一周
- JAVA C# C/C++之比较学:初始化顺序
- Day6
- iOS中网络请求--AFNetworking
- 关于开源分布式文件系统的性能调优方法
- 类和对象 引用属性和方法举例
- Hadoop问题集锦
- vs2013下如何调试main带参数的程序
- 安卓按两次返回键退出程序
- Yii框架的一些系统函数
- normalize()函数的使用方法
- 第2章 一切都为对象
- javaScript-变量
- android studio gradle更新错误