POJ_3176_Cow_Bowling_(数字三角形)_(动态规划)

时间:2022-09-08 20:35:53

描述


http://poj.org/problem?id=3176

给出一个三角形,每个点可以走到它下面两个点,将所有经过的点的值加起来,问最大的和是多少.

Cow Bowling
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16826   Accepted: 11220

Description

The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:
          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

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

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

Sample Output

30

Hint

Explanation of the sample:
          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_(数字三角形)_(动态规划)的更多相关文章

  1. hihoCoder &num;1037 &colon; 数字三角形 (动态规划)

    题目链接:https://hihocoder.com/problemset/problem/1037# 问题描述 小Hi和小Ho在经历了螃蟹先生的任务之后被奖励了一次出国旅游的机会,于是他们来到了大洋 ...

  2. 动态规划略有所得 数字三角形&lpar;POJ1163&rpar;

    在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 三角形的行数大于1小于等于100,数 ...

  3. 动态规划入门——数字三角形(Java)

    动态规划的概念对于新手来说枯燥难懂,就算看懂了,做题的时候依旧抓耳挠腮的毫无头绪,这些比较难理解的算法,还是需要根据例子来一步步学习和理解,从而熟练掌握,下面,咱们就通过一个简单的小例子来学习动态规划 ...

  4. Problem C&colon; 动态规划基础题目之数字三角形

    Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 208  Solved: 139[Submit][Sta ...

  5. &lbrack;动态规划&rsqb;数字三角形&lpar;版本I-III&rpar;

    level 1 1.1题目 1.1.1题目描述 考虑在下面被显示的数字金字塔. 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大.每一步可以走到左下方的点也可以到达右下方的点. 在 ...

  6. &lbrack;蓝桥杯&rsqb;ALGO-124&period;算法训练&lowbar;数字三角形

    问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●<三角形行数≤: ●三角 ...

  7. 动态规划之数字三角形&lpar;POJ1163&rpar;

    在下面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 既然求目标问题是根据查表得来的,自然 ...

  8. vijos 1006 晴天小猪历险记之Hill——数字三角形的终极变化

    题目链接:https://vijos.org/p/1006 数字三角形原题看这里:http://www.cnblogs.com/huashanqingzhu/p/7326837.html 背景 在很久 ...

  9. G&colon;数字三角形

    总时间限制: 1000ms 内存限制: 65536kB描述73   88   1   02   7   4   44   5   2   6   5 (图1) 图1给出了一个数字三角形.从三角形的顶部 ...

随机推荐

  1. hotcss用法

    1.0 GitHub下载地址: https://github.com/imochen/hotcss 或者我的百度网盘:http://pan.baidu.com/s/1pKlLqHX 使用方法看demo ...

  2. 开启flask调试

    直接将app.debug = True时,程序出错并没有出现调试界面. 按照如下设置,flask+uwsgi情况下,python报错时会在浏览器中提示错误信息.方便调试. from werkzeug. ...

  3. lambda表达式对比

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. 通过Java构造参数列表

    背景:我们在进行性能测试时,需要构造测试数据,即参数化文件,如下: 上面的文件内容,我们可以通过Java代码轻松实现,主要代码解释: All 代码(其实我也看不懂,但是会改就行啦) package f ...

  5. PAT 1081 检查密码

    https://pintia.cn/problem-sets/994805260223102976/problems/994805261217153024 本题要求你帮助某网站的用户注册模块写一个密码 ...

  6. 在VSCode中成功安装Go相关插件问题:tools failed to install&period;

    一.介绍 目的:本文将主要介绍在windows使用VSCode配置Go语言环境 软件:VSCode 二.安装出现的问题 完整信息如下 Installing tools at D:\GoPath\bin ...

  7. WebMagic之爬虫监控

    访问我的博客 前言 年前闲着无聊,研究了一阵子爬虫技术,接触到爬虫框架 WebMagic,感觉很好用. 在之后的工作中,接手了新站与第三方接口对接的工作,主要的工作是去抓取对方接口的内容:初始的时候, ...

  8. 文档数据库MongoDB

    MongoDB是一个基于分布式文件存储的文档式数据库.其由C++编写, 旨在为Web应用提供可扩展的高性能数据存储解决方案. MongoDB中每条数据记录被作为一个文档存储,文档由集合(collect ...

  9. redis主从,哨兵(windows版)

    一.下载 由于redis官方并不支持windows操作系统,所以官网上是下不到的,需要到gitlab上下载,下载地址如下: https://github.com/MicrosoftArchive/re ...

  10. (转)粒子编辑器Particle designer属性的介绍

    转载:http://blog.csdn.net/ym19860303/article/details/9210539 Particle designer粒子编辑器可到这里下载(包含授权码):http: ...