Eat potatoesTime Limit: 1000 MS | memory limit: 65535 kb difficulty: 4
-
Description
-
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?
-
Input
-
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.
-
Output
-
For each case, You just output the max qualities you can eat and then get.
-
Sample Input
-
4 611 0 7 5 13 978 4 81 6 22 41 40 9 34 16 1011 22 0 33 39 6
-
Sample output
-
242
-
Source
-
2009 multi-university training contest 4
-
Uploaded
-
Zhang jiejie
Solution: calculate the maximum value of each row first, and then calculate the maximum value again. This question is a bit interesting. Think about it and you can solve it. If you want to hurry up, you can open another array and put the content in the next loop into the read loop.
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