描述

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. python批量下载

    # -*- coding: utf-8 -*-__author__ = 'Administrator'from PyQt4.Qt import *from PyQt4.QtCore import *f ...

  2. 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)

    我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...

  3. Visual Studio 2010 中的 Web 开发

    概述 Microsoft Visual Studio 2010 为 ASP.NET Web 应用程序的开发提供非常多新的功能.这些新功能旨在帮助开发者高速方便地创建和部署质量高且功能全的 Web 应用 ...

  4. [Redux] Redux: Extracting Container Components -- AddTodo

    Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...

  5. C语言的学习-基础知识点

    ---BOOL BOOL BOOL a = YES; printf("%d\n", a); a = NO; printf("%d", a); , b = ; B ...

  6. Oracle用户解锁的三种办法及默认的用户与密码

    ORA-28000: the account is locked-的解决办法 2009-11-11 18:51 ORA-28000: the account is locked 第1步:使用PL/SQ ...

  7. java基础之数组

    数组的定义 数组的应用 1, 2, 3, 4,

  8. p标签里面不要放div标签(块元素)

    最好不要在p标签里面嵌套块级元素(如div Ul): <p>我来测试下<div>块元素</div>放在p标签的情况</p> <p>我来测试下 ...

  9. js调用百度地图搜索功能

    引用百度jsApi <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&a ...

  10. asp.net从服务器(指定文件夹)下载任意格式的文件到本地

    一.我需要从服务器下载ppt文件到本地 protected void Btn_DownPPT_Click(object sender, EventArgs e)        {            ...