吃土豆

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
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 ?

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

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;

int a[505][505];

int main()
{
int m,n;
while(cin>>m>>n)
{
memset(a,0,sizeof(a));
freopen("1.txt","r",stdin);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
int Max;
for(int i=0;i<m;i++)
{
Max=a[i][n-1];
for(int j=n-1;j>=0;j--)
{
a[i][j]+=max(a[i][j+2],a[i][j+3]);
Max=max(Max,a[i][j]);
}
a[i][0]=Max;
}

Max=a[0][0];
a[2][0]+=a[0][0];
for(int i=3;i<m;i++)
{
a[i][0]+=max(a[i-2][0],a[i-3][0]);
Max=max(Max,a[i][0]);
}
cout<<Max<<endl;
}
return 0;
}

思路:

题目大意是如果某个土豆吃了,他的上下左右就不能吃了。

所以可以先对每行求最大。放在每行的行首,

再对第一列求最大。

数的不连续的和的最大值


nyoj 吃土豆的更多相关文章

  1. nyoj 234 吃土豆

    描述 Bean-eating * grid. Now you want to eat the beans and collect the qualities, but everyone must ob ...

  2. nyoj234 吃土豆 01背包

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

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

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

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

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

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

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

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

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

  7. ThreadPoolExecutor 入参 corePoolSize 和 maximumPoolSize 的联系

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

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

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

  9. gensim做主题模型

    作为Python的一个库,gensim给了文本主题模型足够的方便,像他自己的介绍一样,topic modelling for humans 具体的tutorial可以参看他的官方网页,当然是全英文的, ...

随机推荐

  1. CentOS7.6 Install TensorFlow

    1. install pip 1).  yum -y install epel-release 2).  yum install python-pip 3).  pip install --upgra ...

  2. 『PyTorch』第十一弹_torch.optim优化器

    一.简化前馈网络LeNet import torch as t class LeNet(t.nn.Module): def __init__(self): super(LeNet, self).__i ...

  3. 简话Angular 08 Angular ajax

    一句话: 它们Angular框架声明周期的各个阶段,常规约定各专注于特定功能,经过处理也可以互相替换 1.功能细分简解 $http 类似JQuery ajax,支持promise $http.json ...

  4. Vscode设置个人爱好

    Vscode设置个人爱好   插件列表 abusaidm.html-snippets-0.1.0 adamwalzer.string-converter-0.0.9 AESSoft.aessoft-c ...

  5. 068——VUE中vuex的使用场景分析与state购物车实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Tarjan 算法求强联通分量

    转载自:http://blog.csdn.net/xinghongduo/article/details/6195337 还是没懂Tarjan算法的原理.但是感觉.讲的很有道理. 说到以Tarjan命 ...

  7. linux pipes

    In Linux, a pipe is implemented using two filedata structures which both point at the same temporary ...

  8. Scrapy-redis实现分布式爬取的过程与原理

    Scrapy是一个比较好用的Python爬虫框架,你只需要编写几个组件就可以实现网页数据的爬取.但是当我们要爬取的页面非常多的时候,单个主机的处理能力就不能满足我们的需求了(无论是处理速度还是网络请求 ...

  9. Pycharm(五)安装库和虚拟环境

    随便进入一个项目 进入设置    Ctrl + alt + S Project *********  - Project Interpreter     ******是你的项目名字 Project I ...

  10. 使用array_merge重新排列数组下标

    array_merge() 函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的).如果数组是数字索引的,则键名会以连续方式重新索引. 注释:如果仅 ...