描述

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. PHP本地域名解析教程

    1.找到C:\WINDOWS\system32\drivers\etc\hosts 127.0.0.1       localhost 127.0.0.1       www.zhosoft.com ...

  2. 【POJ2114】Boatherds 树分而治之

    做广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog. ...

  3. ARM体系结构_DAY2

    程序状态寄存器(CPSR) Mode位[4:0]:处理器模式为 USER模式不能直接切换到特权模式,在特权模式下可以直接修改mode位[4:0]为10000,切换到USER模式. T bit位[5]: ...

  4. ssh命令

    使用ssh命令登陆远程系统 ssh [ip/address] -l [登陆用户名] 如: ssh www.xyz.cn -l root

  5. CentOS7--DNS处理模块DnsPython的简单使用

    初步了解: DnsPython是Python实现的一个DNS工具包,支持几乎所有的记录类型. 安装: # wget http://www.dnspython.org/kits/1.9.4/dnspyt ...

  6. c#基础: NetWorkStream类的主要属性

    一.网络流 1.    最常用的方法  Read()  Write()    Flush() NetworkStream netStream = new NetworkStream(mesock); ...

  7. asp.net服务器向客户端弹出对话框,但不使页面边白板

    1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; 5: ...

  8. July-程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大经典原创系列集锦与总结

    程序员面试.算法研究.编程艺术.红黑树.数据挖掘5大经典原创系列集锦与总结 http://blog.csdn.net/v_july_v/article/details/6543438

  9. Android -------- 使手机状态栏背景颜色和activity的一致

    Activity类中: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta ...

  10. 脚本化HTTP

    1.HTTP: 定义:超文本传输协议 (HTTP-Hypertext transfer protocol) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协 ...