描述

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 * 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-), (x, y+), and the both rows whose abscissas are x- and x+.

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.
样例输入

样例输出

思路:

考虑对于某行某列元素,row[i][j]表示加上位置为i,j的土豆的质量的i行j列最大的和

列的最大值:row[i][j]=max(row[i][j-2]+row[i][j-3])+val

看图说话:

假设红色的格子为i行j列,那么它的前面有两种选择方案:

1、选择蓝色格子

2、选择黄色格子

那么该行最大的和是什么呢?

由于n列、n-1列具有状态无关性(n-1列的状态影响不了n列的状态),很显然等于max(row[i][n],row[i][n-1])

同理对于dp[i] (i行的最大值)

dp[i]=max(dp[i-2],dp[i-3])+max_row[i]

看图说话:

max土豆质量=max(dp[m],dp[m-1])

为了方便计算,我的代码把n,m扩大了2

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 506
int n,m;
int col[N][N];
int dp[N];
int main()
{
while(scanf("%d%d",&n,&m)==){
memset(col,,sizeof(col));
memset(dp,,sizeof(dp));
for(int i=;i<n+;i++){
for(int j=;j<m+;j++){
int x;
scanf("%d",&x);
col[i][j] = max(col[i][j-],col[i][j-])+x;
}
}
for(int i=;i<n+;i++){
dp[i]=max(dp[i-],dp[i-])+max(col[i][m+],col[i][m+]);
}
printf("%d\n",max(dp[n+],dp[n+]));
}
return ;
}

nyoj 234 吃土豆的更多相关文章

  1. nyoj 吃土豆

    吃土豆 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Bean-eating is an interesting game, everyone owns an M* ...

  2. nyoj234 吃土豆 01背包

    思路:假设我们先只考虑一行,规则就是取了i处的土豆,每一个土豆有两种选择,拿与不拿,那么i-1和i+1处的土豆都不能再取,那么要求某一行的最大取值就用一次动态规划即可,dp(i)表示前i个土豆能取得的 ...

  3. nyoj 对决吃桃

    时间限制:3000 ms  |  内存限制:65535 KB 难度:0   描述 有一堆桃子不知数目,猴子第一天吃掉一半,又多吃了一个,第二天照此方法,吃掉剩下桃子的一半又多一个,天天如此,到第m天早 ...

  4. 设计模式(二)简单工厂模式(Simple Factory Pattern)

    一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理解的模式——简单工厂模式. 二.简单工厂 ...

  5. head first 设计模式读书笔记 之 策略模式

    作为一个php开发者,深知曾经很多程序员都鄙视php,为什么呢?因为他们认为php的语法是dirty的,并且由于开发者水平参差不齐导致php的代码更加乱上加乱,维护起来简直一坨shit一样.随着php ...

  6. C#设计模式之二简单工厂模式(过渡模式)

    一.引言 之所以写这个系列,是了为了自己更好的理解设计模式,也为新手提供一些帮助,我都是用最简单的.最生活化的实例来说明.在上一篇文章中讲解了单例模式,今天就给大家讲一个比较简单的模式--简单工厂模式 ...

  7. gensim和jieba分词进行主题分析,文本相似度

    参考链接:https://blog.csdn.net/whzhcahzxh/article/details/17528261 demo1:结巴分词: # 构造分词库,格式如下: ''' [['楼下', ...

  8. ThreadPoolExecutor 入参 corePoolSize 和 maximumPoolSize 的联系

    前言 我们可以通过 java.util.concurrent.ThreadPoolExecutor 来创建一个线程池: new ThreadPoolExecutor(corePoolSize, max ...

  9. C#设计模式(2)——简单工厂模式(转)

    C#设计模式(2)——简单工厂模式   一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理 ...

随机推荐

  1. 马士兵 Servlet_JSP(2) JSP源代码)

    1.最简单的JSP HelloWorld.jsp <html>     <head>         <title>Hello</title>     ...

  2. [Qt] fontawesome图标

    fontawesome图标 fontawesome是一个图标的集合,里面有好多的图标,使用起来也还是非常方便的. 图标信息可以到官网去查:http://fontawesome.io/cheatshee ...

  3. java遍历Hashmap/Hashtable的几种方法

    一>java遍历Hashtabe: import java.util.Hashtable; import java.util.Set; public class HashTableTest { ...

  4. WebService-使用JDK开发WebService

    一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...

  5. debian 源

    把下面的源覆盖在/etc/apt/sources.list deb http://http.debian.net/debian wheezy main deb-src http://http.debi ...

  6. View事件传递之父View和子View之间的那点事

    Android事件传递流程在网上可以找到很多资料,FrameWork层输入事件和消费事件,可以参考: Touch事件派发过程详解 这篇blog阐述了底层是如何处理屏幕输,并往上传递的.Touch事件传 ...

  7. c# 鼠标操作

    1#region 3using System; 4using System.Runtime.InteropServices; 6#endregion 8namespace Windows.Forms. ...

  8. trailingZeroes

    Given an integer n, return the number of trailing zeroes in n!. 给一个数字n,返回它n!数字后面有多少个0. public class ...

  9. 通过 sp_configure 进行 Database Mail 配置

    通过 sp_configure 进行 Database Mail 配置 直接执行步骤一. 如果报错,则先执行步骤二,再执行步骤一. 一. sp_configre ; GO RECONFIGURE; G ...

  10. USB联机线编程接口(API)

    USB联机线编程接口(API) 2013-10-19 本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用. 关键字:USB隔离线.USB点对点通讯.USB通 ...