Beans

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
 思路:
STEP_1:先以行为单位来算,设DP数组存的是以此位置为起点能得到的最大值,在此前提下,DP[i][j]因为相邻的不能走,所以要么加上DP[i][j + 2],要么加上DP[i][j + 3] (假设不越界),取最大的那个就好。从右往左依次计算这一行每个元素的DP值,记录下最大的,放到DP[i][0]里。
STEP_2:重复第一步,只不过对象换成了DP数组里每行0号单元的值,因为此单元放的是此行内的最大值。从上往下,每次选定一行后设此行为最终答案里最下面那行,因为选定一行之后它的上面和下面那一行就废掉了,所以每一行要么加上它上面第二行,要么加上它上面第三行。同理,记录下最大值,最大值即答案。(实际上答案不是最后一行就是倒数第二行,因为没有负数,只会越加越大,不过只有1行的时候就会越界,虽然HDU上的数据貌似没1行的)。
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200105 int main(void)
{
int n,m;
int max,temp_1,temp_2,ans; while(scanf("%d%d",&n,&m) != EOF)
{
int dp[n + ][m + ]; memset(dp,,sizeof(dp));
for(int i = ;i <= n;i ++)
for(int j = ;j <= m;j ++)
scanf("%d",&dp[i][j]); for(int i = ;i <= n;i ++)
{
max = dp[i][m];
for(int j = m;j >= ;j --)
{
dp[i][j] += dp[i][j + ] > dp[i][j + ] ? dp[i][j + ] : dp[i][j + ];
max = max > dp[i][j] ? max : dp[i][j];
}
dp[i][] = max;
} max = dp[][];
dp[][] += dp[][];
for(int i = ;i <= n;i ++)
{
dp[i][] += dp[i - ][] > dp[i - ][] ? dp[i - ][] : dp[i - ][];
max = max > dp[i][] ? max : dp[i][];
} printf("%d\n",max);
} return ;
}
 

HDU 2845 Beans (DP)的更多相关文章

  1. HDU 2845 Beans (两次线性dp)

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. HDU 2845 Beans (DP)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  3. hdu 2845——Beans——————【dp】

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. HDU 2845 Beans(dp)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  5. HDU 2845 Beans (动态调节)

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  6. Hdu 2845 Beans

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. hdu 2845 Beans 2016-09-12 17:17 23人阅读 评论(0) 收藏

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  8. hdu 2845 Beans(最大不连续子序列和)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  9. hdu 2845简单dp

    /*递推公式dp[i]=MAX(dp[i-1],dp[i-2]+a[j])*/ #include<stdio.h> #include<string.h> #define N 2 ...

随机推荐

  1. 如何让windows服务器IIS支持.apk/.ipa文件下载

    打开IIS服务管理器,找到服务器,右键-属性,打开IIS服务属性: 单击MIME类型下的“MIME类型”按钮,打开MIME类型设置窗口: 单击“新建”,建立新的MIME类型: 扩展名是:.apk MI ...

  2. 在Windows2012下安装SQL Server 2005无法启动服务的解决办法

    下面是我亲自经历过的总结. 因为尝鲜安装了Windows2012,的确很不错,唯一的遗憾就是不支持Sql Server 2005的安装.找了很多办法,基本上都有缺陷.现在终于找到一种完全正常没有缺陷的 ...

  3. (剑指Offer)面试题23:从上到下打印二叉树

    题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 很明显,这是一个广度优先遍历. 需要一个队列容器来保存结点,具体操作: 1.将根结点压入队列中,并打印根结点:如果根结点有子结点 ...

  4. map 转换 xml ; xml转map

    public class MessageKit { public static String map2xml(Map<String, String> map) throws IOExcep ...

  5. SetWindowsHookEx 相关

    SetWindowsHookEx function https://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85) ...

  6. codeforces 377A. Puzzles 水题

    A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...

  7. [置顶] Android开发之MediaPlayerService服务详解(一)

    前面一节我们分析了Binder通信相关的两个重要类:ProcessState 和 IPCThreadState.ProcessState负责打开Binder 驱动,每个进程只有一个.而 IPCThre ...

  8. JS可以做什么,它的能力范围 View----------Request/Submit------------------Server

    View----------Request/Submit------------------Server javascript--------><script>标签方式(页面,动态插 ...

  9. iOS开发——路径篇&混编路径与全局宏路径

    混编路径与全局宏路径 最近在做东西的时候有一个地方要用到一个第三方库的,但是目前swift版的还没有找到,自己又不想写(其实是不会写),所以就想到了混编,但是中间出现了好多问题,其中印象最深的就是桥接 ...

  10. Xcode和IOS模拟器

    Xcode和IOS模拟器 目录 概述 Xcode常用操作 学会用Instrument IOS模拟器 概述 Xcode常用操作 整体缩进或者缩退 command+“[” .command+“]” 在同一 ...