hdu 2845 Beans

Beans

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3682    Accepted Submission(s): 1762


Problem 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<=200000.
 

 

Output
For each case, you just output the MAX qualities you can eat and then get.
 

 

Sample Input
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
 

 

Sample Output
242
 

 

Source
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2830 2577 2870 2844 1069 
 

一道很明显的dp题,隔行隔列相加之和最大,同样的dp列式使用两次。

 

题意:取其中一个数,则不能取相邻的两个数,最后取出数字之和为每行的最大子序列之和。

再在每行的子序列之和中取出几个数字,规则和取行数字相同,不能取相邻的数,最后之和为输出最终结果。

 

附上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #define N 200005
 5 using namespace std;
 6 int a[N],b[N],dp[N];
 7 int main()
 8 {
 9     int m,n,i,j;
10     while(~scanf("%d%d",&n,&m))
11     {
12         dp[0]=a[0]=b[0]=0;    //初始化
13         for(i=1; i<=n; i++)
14         {
15             for(j=1; j<=m; j++)
16                 scanf("%d",&a[j]);
17             dp[1]=a[1];
18             for(j=2; j<=m; j++)
19                 dp[j]=max(dp[j-2]+a[j],dp[j-1]);   //dp思想,取结果大的数据
20             b[i]=dp[m];     //b数组记录每一行的最大子序列和
21         }
22         dp[1]=b[1];
23         for(i=2; i<=n; i++)
24             dp[i]=max(dp[i-2]+b[i],dp[i-1]);   //完全一样的思想,只是前面去行的最大值,这里取列的最大值
25         printf("%d\n",dp[n]);
26     }
27     return 0;
28 }

 

posted @ 2015-08-23 14:03  lucky_少哖  阅读(161)  评论(0编辑  收藏  举报