码迷,mamicode.com
首页 > 其他好文 > 详细

NYOJ 234 吃土豆

时间:2014-07-16 12:01:49      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   strong   os   

吃土豆

时间限制: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 吃土豆

标签:style   blog   http   color   strong   os   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!