http://www.lydsy.com/JudgeOnline/problem.php?id=1668

裸dp。。

f[i][j]表示i行j列最大能拿到

f[i][j]=max(f[i+1][j-1], f[i-1][j-1], f[i][j-1])+a[i][j] 当f[i+1][j-1], f[i-1][j-1], f[i][j-1]均不为0时

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb] << '\t'; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } int mp[105][105], n, m, f[105][105]; int main() {
read(n); read(m);
for1(i, 1, n) for1(j, 1, m) read(mp[i][j]);
f[1][1]=mp[1][1];
for1(j, 2, m) for1(i, 1, n) {
if(i>1) f[i][j]=max(f[i][j], f[i-1][j-1]);
if(i<n) f[i][j]=max(f[i][j], f[i+1][j-1]);
f[i][j]=max(f[i][j], f[i][j-1]);
if(f[i-1][j-1] || f[i+1][j-1] || f[i][j-1]) f[i][j]+=mp[i][j];
}
print(f[n][m]);
return 0;
}

Description

最 近,奶牛们热衷于把金币包在面粉里,然后把它们烤成馅饼。第i块馅饼中含有Ni(1<=Ni<=25)块金币,并且,这个数字被醒目地标记在 馅饼表面。 奶牛们把所有烤好的馅饼在草地上排成了一个R行(1<=R<=100)C列(1<=C<=100)的矩阵。你现在站在坐标为 (1,1)的馅饼边上,当然,你可以拿到那块馅饼里的所有金币。你必须从现在的位置,走到草地的另一边,在坐标为(R,C)的馅饼旁边停止走动。每做一次 移动,你必须走到下一列的某块馅饼旁边,并且,行数的变动不能超过1(也就是说,如果现在你站在坐标为(r,c)的馅饼边上,下一步你可以走到坐标为 (r-1,c+1),(r,c+1),或者(r+1,c+1)的馅饼旁边)。当你从一块馅饼边经过,你就可以拿走馅饼里所有的金币。当然啦,你一定不会愿 意因半路离开草地而失去唾手可得的金币,但,最终你一定得停在坐标为(R,C)的馅饼旁边。 现在,你拿到了一张标记着馅饼矩阵中,每一块馅饼含金币数量的表格。那么,按照规则,你最多可以拿到多少金币呢? 比方说,奶牛们把馅饼排成如下的矩阵,矩阵中的数字表示该位置的馅饼中含金币的数量:

6 5 3 7 9 2 7
2 4 3 5 6 8 6
4 9 9 9 1 5 8

以下是条合法的路线

按上述的路线进行走动,一共可以获得6+4+9+9+6+5+8=47个金币.按照规则,在这个矩阵中最多可以得到50个金币,路线如下图所示:

Input

* 第1行: 两个用空格隔开的整数,R和C

* 第2..R+1行: 每行包含C个用空格隔开的正整数,依次表示一行中从左往右各 个馅饼里金币的数量

Output

* 第1行: 输出一个正整数,表示你所能收集到的最大金币数目

Sample Input

3 7
6 5 3 7 9 2 7
2 4 3 5 6 8 6
4 9 9 9 1 5 8

Sample Output

50

HINT

Source

【BZOJ】1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富(dp)的更多相关文章

  1. BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富( dp )

    dp , dp[ i ][ j ] = max( dp[ k ][ j - 1 ] ) + G[ i ][ j ] ( i - 1 <= k <= i + 1 , dp[ k ][ j - ...

  2. BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富

    Description 最近,奶牛们热衷于把金币包在面粉里,然后把它们烤成馅饼.第i块馅饼中含有Ni(1<=Ni<=25)块金币,并且,这个数字被醒目地标记在馅饼表面. 奶牛们把所有烤好的 ...

  3. bzoj 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富【记忆化搜索+剪枝】

    c[x][y]为从(x,y)到(n,m)的最大值,记忆化一下 有个剪枝是因为y只能+1所以当n-x>m-y时就算x也一直+1也是走不到(n,m)的,直接返回0即可 #include<ios ...

  4. 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富

    1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 498  Sol ...

  5. BZOJ1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富

    1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 459  Sol ...

  6. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...

  7. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

    有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...

  8. BZOJ 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛( LIS )

    裸的LIS ----------------------------------------------------------------- #include<cstdio> #incl ...

  9. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

随机推荐

  1. Servlet之生命周期【入门版(刚開始学习的人必看)】

    6,Servlet的解说 6.1Servlet生命周期 ,继承HttpServlet init方法(初始化Servlet)将来能够做一些初始化工作 service方法(处理请求) 一般不须要重写ser ...

  2. Android Exception 5(startActivityForResult & singleTask)

    昨天碰到一个很诧异的事情,主要内容是1.startActivityForResult(intent2, 302);2.onActivityResult 问题是:onActivityResult调用时间 ...

  3. php 一个过虑xxs的代码

    一个过虑xxs的代码 public static function removeXSS($str) { $str = str_replace('<!-- -->', '', $str); ...

  4. BOM*创建工艺路线

    --工艺路线 DECLARE -- API input variables l_operation_tbl bom_rtg_pub.operation_tbl_type := bom_rtg_pub. ...

  5. 滚动居中效果(frame版)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 5分钟教你配置命令行界面的163邮箱client

    网易163免费邮箱相关server信息: mutt 是命令行下的邮件client,它仅是管理邮件的组织编辑,邮件的发送和接收须要调用相应的程序. 这里用的各自是getmail 和 msmtp. 过程例 ...

  7. 安装 Percona XtraBackup 2.3

    Installing Percona XtraBackup from Percona yum repository Install the Percona repository You can ins ...

  8. Linux命令-压缩解压命令:tar

    tar [选项] [打包后文件名] [打包前的文件或者目录名称] -c表示创建(create-创建) -z表示压缩(gzip-压缩) -j表示压缩(bzip2-压缩) -v显示进度(verbose-冗 ...

  9. 加解密(校验哈希值、MD5值)

    1.计算文本哈希值: public static string ComputeHash(string password) { byte[] buffer = System.Text.Encoding. ...

  10. hdu 4779 Tower Defense (思维+组合数学)

    Tower Defense Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...