poj1163 the triangle 题解
Description
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
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
题目大意:求出三角形的最大和,规定路径从第一行第一个为起点,路径为向下一行的该列或者下一行的右边的一列(在所有数字向左对齐的情况下),终点为最后一行的某个数。
解题思路:这是一道dp教程中十分常见的入门题,在解这道题目我们要尽量防止回溯,因为很有可能会超时(实际上也超了),所以我们十分需要用一个数组来储存过程中计算得到的结果,在代码里面我定义为dp数组,dp[i][j]表示从倒数第一排开始走到i排j列的最大和,如果输入是样例输入的话,那么dp数组应该是这样的。
30
23 21
20 13 10
7 12 10 10
4 5 2 6 5
(其余为0)
递归代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define MAXN 100
using namespace std;
int map[MAXN + 5][MAXN + 5];
int dp[MAXN + 5][MAXN + 6];
int n;
int sum; int maxsum(int x, int y)
{
if(dp[x][y] != -1)//dp[x][y]!=-1表示已经计算了,不必再算
return dp[x][y]; if(x == n)//x==n表示最后一行,最小和为map[x][y]他本身
{
dp[x][y] = map[x][y];
}
else
{
int i = maxsum(x + 1, y);
int j = maxsum(x + 1, y + 1);
dp[x][y] = max(i, j) + map[x][y];
}
return dp[x][y];
} int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= i; j ++)
{
scanf("%d", &map[i][j]);
dp[i][j] = -1;
}
}
cout << maxsum(1, 1) << endl;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j ++)
{
cout << dp[i][j] << ' ';
}
cout << endl;
}
return 0;
}
/*
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
ans:30
*/
递推代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define MAXN 100
using namespace std;
int map[MAXN + 5][MAXN + 5];
int dp[MAXN + 5][MAXN + 6];
int n;
int sum; int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= i; j ++)
{
scanf("%d", &map[i][j]);
}
} for(int i = 1; i <= n; i ++)//最后一行的最大和是最后一行的数字本身
dp[n][i] = map[n][i]; for(int i = n - 1; i >= 1; i --)//从最后一排到第一排
{
for(int j = 1; j <= i; j ++)
{
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + map[i][j];
}
}
cout << dp[1][1] << endl;
return 0;
}
/*
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
ans:30
*/
递推代码的空间优化:
空间优化的思路:将dp从二维转换为一维很明显能节省很多空间,dp[i]这时候表示第一行i列为终点的最大和
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define MAXN 100
using namespace std;
int map[MAXN + 5][MAXN + 5];
int dp[MAXN + 5];
int n;
int sum; int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= i; j ++)
{
scanf("%d", &map[i][j]);
}
}
for(int i = 1; i <= n; i ++)//最后一行的最大和是最后一行的数字本身
{
dp[i] = map[n][i];
}
for(int i = n - 1; i >= 1; i --)//从最后一排到第一排
{
for(int j = 1; j <= i; j ++)
{
dp[j] = max(dp[j], dp[j + 1]) + map[i][j];
}
}
cout << dp[1] << endl;
return 0;
}
/*
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
ans:30
*/
poj1163 the triangle 题解的更多相关文章
- ZOJ 4081 Little Sub and Pascal's Triangle 题解
ZOJ 4081 Little Sub and Pascal's Triangle 题解 题意 求杨辉三角第n行(从1开始计数)有几个奇数. 考察的其实是杨辉--帕斯卡三角的性质,或者说Gould's ...
- POJ1163 The Triangle 【DP】
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36918 Accepted: 22117 De ...
- POJ1163——The Triangle
Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program t ...
- codechef Sums in a Triangle题解
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...
- (数字三角形)POJ1163 The Triangle
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59698 Accepted: 35792 De ...
- POJ1163 The Triangle
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44997 Accepted: 27174 Description 73 ...
- Poj1163 The Triangle(动态规划求最大权值的路径)
一.Description 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 pro ...
- CF336A Vasily the Bear and Triangle 题解
Content 一个矩形的顶点为 \((0,0)\),其对顶点为 \((x,y)\),现过 \((x,y)\) 作直线,分别交 \(x\) 轴和 \(y\) 轴于 \(A,B\) 两点,使得 \(\t ...
- poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)
经典的数塔模型. 动态转移方程: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...
- POJ1163 The Triangle: 倒三角形问题
经典的DP问题,DP思想也很直接: 直接贴代码: #include<iostream> #include<cstdio> #include<algorithm> # ...
随机推荐
- dotnet OpenXML 文本删除线解析方法
本文来告诉大家如何解析读取在 OpenXML 里面存放的文本删除线,本文使用 PowerPoint 作为例子来告诉大家如何读取然后在 WPF 应用里面显示 在开始之前,期望大家已了解如何在 dotne ...
- SpringBoot中几种好用的代码生成器(基于Mybatis-plus生成entity、mapper、xml等)
前言 熟悉Spring框架的同学一定都知道MVC开发模式吧,控制器(Controller).业务类(Service).持久层(Repository).数据库映射(Mapper).各种DO类构成了我们服 ...
- R_回归模型实例一
工作和生活中存在大量的具有相关性的事件,当找到不同变量之间的关系,我们就会用到回归分析.回归分析(Regression Analysis):是用来确定2个或2个以上变量间关系的一种统计分析方法. 在回 ...
- VMware虚拟机安装Centos-7.9
VMware虚拟机安装Centos-7.9 创作不易,点赞关注一下吧 1.安装VMware Workstation Pro 大家根据自己的实际情况安装合适版本的VMware Workstation P ...
- golang中三种定时器的实现方式及周期定时
一.定时器的创建 golang中定时器有三种实现方式,分别是time.sleep.time.after.time.Timer 其中time.after和time.Timer需要对通道进行释放才能达到定 ...
- PageOffice 在线编辑 office文件,回调父页面
一.子页面调用父页面的方法 var value=window.external.CallParentFunc("ParentFunName(Arguments);");//父页面的 ...
- Chrome:用uBlacklist屏蔽CSDN搜索结果
CSDN现在广告满天飞,且很多博客需要先关注才能复制,非常令人无语.如果每次用Google搜索的时候都要加上"-csdn"选项,就非常麻烦.有没有更方便的办法呢?我们可以利用Chr ...
- Semantic Kernel入门系列:利用Handlebars创建Prompts functions
引言 本章我们将学习通过Handlebars Prompts Template来创建Prompts functions. 什么是Handlebars? Handlebars是一个流行的 JavaScr ...
- Windows中实现将bat或exe文件作为服务_且实现命令行安装、配置、启动、删除服务
一.背景描述 在Windows环境下进行日常的项目开发过程中,有时候需要将bat文件或exe文件程序注册为Windows的服务实现开机自己运行(没有用户登陆,服务在开机后也可以照常运行).且对于那些没 ...
- 微软的文本转语音服务Microsoft.CognitiveServices.Speech
微软的Edge 浏览器里的大声朗读里-"晓晓" 很接近自然人,比起其它平台的强很多. 在AZURE 可免费体验,每月限额50万字,每个语音转换不超过10分钟长度. C# 调用: u ...