0
点赞
收藏
分享

微信扫一扫

hdu2845 Beans--DP


原题链接: ​​ http://acm.hdu.edu.cn/showproblem.php?pid=2845​​


一:原题内容


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.


hdu2845 Beans--DP_C++



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



二:分析理解

《压缩元素,借助递推》
    首先,对于每一行,求出如果从这行挑元素(因为某个元素
被挑的话,它的上下行都不可以再被挑,而自身所在的那行还有
元素可以挑),可以得到的sum[i],这样“就把这行m个数压缩为
一个元素了”;
    那么,对于n行都进行上面说的压缩(进行了n次),再竖着
这对着n个小sum进行一次同样的压缩,那么就得到了一个大SUM,

三:AC代码

#include<iostream>  
#include<string.h>
#include<algorithm>

using namespace std;

int a[200005];
int b[200005];

int main()
{
int n, m, x;

while (~scanf("%d%d", &n, &m))
{
for (int i = 2; i <= n + 1; i++)
{
for (int j = 2; j <= m + 1; j++)
{
scanf("%d", &x);

a[j] = max(a[j - 1], a[j - 2] + x);
}

b[i] = max(b[i - 1], b[i - 2] + a[m + 1]);
}

printf("%d\n", b[n + 1]);
}


return 0;
}



举报

相关推荐

0 条评论