POJ_3176_Cow_Bowling_(数字三角形)_(动态规划)
描述
http://poj.org/problem?id=3176
给出一个三角形,每个点可以走到它下面两个点,将所有经过的点的值加起来,问最大的和是多少.
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16826 | Accepted: 11220 |
Description
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.
Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.
Input
Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
Hint
7
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
The highest score is achievable by traversing the cows as shown above.
Source
分析
记忆化搜索:
#include<cstdio>
#include<cstring>
#include<algorithm>
using std :: max; const int maxn=;
int a[maxn][maxn],f[maxn][maxn];
int n; int dfs(int i,int j)
{
if(f[i][j]!=-) return f[i][j];
if(i==n) return f[i][j]=a[i][j];
return f[i][j]=a[i][j]+max(dfs(i+,j),dfs(i+,j+));
} void init()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
memset(f,-,sizeof(f));
} int main()
{
#ifndef ONLINE_JUDGE
freopen("cow.in","r",stdin);
freopen("cow.out","w",stdout);
#endif
init();
printf("%d\n",dfs(,));
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
动态规划:
#include<cstdio>
#include<algorithm>
using std :: max; const int maxn=;
int n;
int a[maxn][maxn],f[maxn][maxn]; void solve()
{
for(int i=n-;i>=;i--)
{
for(int j=;j<=i;j++)
{
f[i][j]=max(f[i+][j],f[i+][j+])+a[i][j];
}
}
printf("%d\n",f[][]);
} void init()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int j=;j<=n;j++) f[n][j]=a[n][j];
} int main()
{
#ifndef ONLINE_JUDGE
freopen("cow.in","r",stdin);
freopen("cow.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
POJ_3176_Cow_Bowling_(数字三角形)_(动态规划)的更多相关文章
- hihoCoder #1037 : 数字三角形 (动态规划)
题目链接:https://hihocoder.com/problemset/problem/1037# 问题描述 小Hi和小Ho在经历了螃蟹先生的任务之后被奖励了一次出国旅游的机会,于是他们来到了大洋 ...
- 动态规划略有所得 数字三角形(POJ1163)
在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 三角形的行数大于1小于等于100,数 ...
- 动态规划入门——数字三角形(Java)
动态规划的概念对于新手来说枯燥难懂,就算看懂了,做题的时候依旧抓耳挠腮的毫无头绪,这些比较难理解的算法,还是需要根据例子来一步步学习和理解,从而熟练掌握,下面,咱们就通过一个简单的小例子来学习动态规划 ...
- Problem C: 动态规划基础题目之数字三角形
Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 208 Solved: 139[Submit][Sta ...
- [动态规划]数字三角形(版本I-III)
level 1 1.1题目 1.1.1题目描述 考虑在下面被显示的数字金字塔. 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大.每一步可以走到左下方的点也可以到达右下方的点. 在 ...
- [蓝桥杯]ALGO-124.算法训练_数字三角形
问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●<三角形行数≤: ●三角 ...
- 动态规划之数字三角形(POJ1163)
在下面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 既然求目标问题是根据查表得来的,自然 ...
- vijos 1006 晴天小猪历险记之Hill——数字三角形的终极变化
题目链接:https://vijos.org/p/1006 数字三角形原题看这里:http://www.cnblogs.com/huashanqingzhu/p/7326837.html 背景 在很久 ...
- G:数字三角形
总时间限制: 1000ms 内存限制: 65536kB描述73 88 1 02 7 4 44 5 2 6 5 (图1) 图1给出了一个数字三角形.从三角形的顶部 ...
随机推荐
- 解决OOM小记
跟猜想的一样是OOM.一回来遇一不怎么熟悉的sb,给我气的....算了.....哥哥也是种种原因回的合肥.继续看问题. 这个地方的界面是这样的 划红线的地方是三个LinearLayout,每次oncl ...
- ASP.NET页面生命周期总结(2)
HttpAplicationFactory获取一个HttpApplication对象: 内部:1.如果是第一次请求过来,那么就把global文件编译成一个类型.(后续请求来的,就可以直接获取这个类型) ...
- 15_AOP入门准备_静态代理模式
[工程截图] [PersonDao.java] package com.HigginCui.daoProxy; public interface PersonDao { public void sav ...
- LA 3902 Network(树上最优化 贪心)
Network Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes ...
- devenv 命令用法
devenv是VisualStudio的可执行程序,一般安装在“C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE”下. 这 ...
- go build 时报错 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
最近在玩Go win下尝试编译Go的时候遇到了下面提示(可能是gorocksdb用到了gcc) gcc也需要64位的 最后找到了个帖子: https://github.com/mattn/go-sql ...
- ubuntu上 安装 基于sphinx 的 coreseek 全文搜索
原生sphinx不支持中文, sphinx-for-chinese匹配中文时也不返回结果 ,真纠结, 最好试了 coreseek,这个能正确返回结果了, 所以记录一下 1 http://www.co ...
- 国内各大互联网公司UED(用户体验设计)团队博客介绍
UED是什么UED = user experience design,用户体验设计.UED的通常理解,就是“我们做的一切都是为了呈现在您眼前的页面”.UED团队包括:交互设计师(Interactio ...
- 一个PDO类
下面是在网上借鉴的一个PDO类: <?php class Database{ private $host = DB_HOST; private $user = DB_USER; private ...
- 向OC类中添加默认的协议实现(ProtocolKit)
以forkingdog的PorotocolKit举例 举例 ProtocolKit Protocol extension for Objective-C Usage Your protocol: @p ...