吃土豆
时间限制: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
- 来源
- 2009 Multi-University Training Contest 4
- 上传者
- 张洁烽
解题:先求每一行的最大值,然后把这些最大值再求一次最大值。这道题目还是有点意思啦。慢慢想想还是可以解出来的。想再快点的话可以再开个数组,把后面那个循环里面的内容放到读入循环里面去。
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 }
NYOJ 234 吃土豆
时间: 2024-10-19 12:45:18