The Triangle (简单动态规划)
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5 (Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
Input
Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.
Output
Your program is to write to standard output. The highest sum is written as an integer.
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
题解:求取数字三角形从顶到底和最大的一条路,每次只能往左下或右下走,那我们的思路就是从底到顶动态规划即可,每次只需要取最大的,那到顶一定是最大的
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
int dp[105][105];
for(int t=0;t<n;t++)
{
for(int j=0;j<=t;j++)
{
scanf("%d",&dp[t][j]);
}
}
for(int t=n-2;t>=0;t--)
{
for(int j=0;j<=t;j++)
{
dp[t][j]+=max(dp[t+1][j+1],dp[t+1][j]);
}
}
printf("%d\n",dp[0][0]);
return 0;
}
The Triangle (简单动态规划)的更多相关文章
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- 简单动态规划——三逆数的O(N^2)解法!
[算法]简单动态规划——三逆数的O(N^2)解法! 问题描述: 三逆数定义:给一个数的序列A[0,1,....N-1]),当i<j<k且A[i]>A[j]>A[k]时,称作ai ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 【算法】简单动态规划——三逆数的O(N^2)解法!
问题描述: 三逆数定义:给一个数的序列A[0,1,....N-1]),当i<j<k且A[i]>A[j]>A[k]时,称作ai,aj,ak为一个三逆数. 现在给定一个长度为N的数 ...
- 51nod 1270 数组的最大代价 思路:简单动态规划
这题是看起来很复杂,但是换个思路就简单了的题目. 首先每个点要么取b[i],要么取1,因为取中间值毫无意义,不能增加最大代价S. 用一个二维数组做动态规划就很简单了. dp[i][0]表示第i个点取1 ...
- HDU 1176 免费馅饼 简单动态规划
世道很简单的动态规划,但是却错了,让我很无语,改来改去还是不对,第二天有写就对了,之后我就耐着性子慢慢比较之前的错误代码,发现 第一次错:纯粹用了a[i][j]+=max3(a[i+1][j-1], ...
- POJ - 1163 The Triangle 【动态规划】
一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...
- HDUOJ----2571(命运)(简单动态规划)
命运 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- Triangle(动态规划)
题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
随机推荐
- AsyncTask被废弃了,换Coroutine吧
本文主要是学习笔记,有版权问题还请告知删文 鸣谢:guolin@第一行代码(第三版) 你是否也在最近的代码中看见了 AsyncTask 被一条横杠划掉了 这表明--他要被Google放弃了 Googl ...
- JavaScript中为什么需要!!?
1. 布尔值为false的值 在JavaScript中,布尔值为 false的值有如下几个: undefined null false 0 NaN ""或''(空字符串) 2. ! ...
- HRNet
- 商业分析-04行为&业务相关数据指标
[访问深度]用户对产品的了解程度 [弹出率] 弹出率是基于访问回话的 而不是基于页面的,上图中1 4 6 是属于弹出
- Qt之先用了再说系列-定时器的用法
Qt 定时器是一个比好用的东西,在此就地简单记录一下使用的方式: 1.首先包含头文件 #include <QTimer> 2.具体用法有几种,我就写其中2种吧 1>执行一次 这种方式 ...
- Flutter 打包程序 build android apk
Step-1 Java 路径 找到java路径, 可使用[flutter doctor -v] Step-2: 进入目录 找到路径后 C:\Program Files\Java\jre1.8.0_23 ...
- Java多线程编程(6)--线程间通信(下)
因为本文的内容大部分是以生产者/消费者模式来进行讲解和举例的,所以在开始学习本文介绍的几种线程间的通信方式之前,我们先来熟悉一下生产者/消费者模式. 在实际的软件开发过程中,经常会碰到如下场景 ...
- Remix+Geth 实现智能合约部署和调用详解
Remix编写智能合约 编写代码 在线调试 实现部署 调用接口 Geth实现私有链部署合约和调用接口 部署合约 调用合约 获得合约实例 通过实例调用合约接口 Remix编写智能合约 编写代码 Remi ...
- Android 开发学习进程0.13 Androidstudio快捷键 xmlns
xmlns XML namespace xml命名空间 其中主要是定义xml文件定义位置 前缀有三种,android app tools 后面为唯一标识符URI android 表示为引用自安卓系统 ...
- 《MySQL必知必会》简介、使用
2.MySQL简介 2.1 什么是MySQL 我们在前一-章中介绍了数据库和SQL.正如所述,数据的所有存储. 检索.管理和处理实际上是由数据库软件一 DBMS (数据库管理系统) 完成的. MySQ ...