首页 > 其他 > 详细

NYOJ 234 吃土豆

时间:2014-07-16 17:01:26      阅读:371      评论:0      收藏:0      [点我收藏+]

吃土豆

时间限制: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.
bubuko.com,布布扣


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
来源
2009 Multi-University Training Contest 4
上传者
张洁烽

  

解题:先求每一行的最大值,然后把这些最大值再求一次最大值。这道题目还是有点意思啦。慢慢想想还是可以解出来的。想再快点的话可以再开个数组,把后面那个循环里面的内容放到读入循环里面去。

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <climits>
 5 using namespace std;
 6 int d[510][510],dp[510][2];
 7 int mx[510];
 8 int main() {
 9     int r,c,i,j,temp,ans;
10     while(~scanf("%d %d",&r,&c)) {
11         for(i = 1; i <= r; i++) {
12             memset(dp,0,sizeof(dp));
13             mx[i] = INT_MIN;
14             for(j = 1; j <= c; j++) {
15                 scanf("%d",d[i]+j);
16                 dp[j][0] = max(dp[j-1][0],dp[j-1][1]);
17                 dp[j][1] = dp[j-1][0] + d[i][j];
18                 temp = max(dp[j][0],dp[j][1]);
19                 mx[i] = max(temp,mx[i]);
20             }
21         }
22         memset(dp,0,sizeof(dp));
23         ans = INT_MIN;
24         for(i = 1; i <= r; i++) {
25             dp[i][0] = max(dp[i-1][0],dp[i-1][1]);
26             dp[i][1] = dp[i-1][0] + mx[i];
27             temp = max(dp[i][0],dp[i][1]);
28             if(temp > ans) ans = temp;
29         }
30         printf("%d\n",ans);
31     }
32     return 0;
33 }
View Code

 

 

NYOJ 234 吃土豆,布布扣,bubuko.com

NYOJ 234 吃土豆

原文:http://www.cnblogs.com/crackpotisback/p/3848000.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!