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> # ...
随机推荐
- SpringMVC学习二(日期参数/数据保存/重定向)
接受的参数为日期类型 controller进行数据保存 Controller如何进行重定向跳转 1.对于前端页面传来日期类型的数据时如何进行处理,有两种方法 1.1在对应的Controller中插入代 ...
- XML Schema(XSD)详解:定义 XML 文档结构合法性的完整指南
XML Schema描述了 XML 文档的结构.XML Schema语言也称为 XML Schema Definition(XSD). <?xml version="1.0" ...
- ACM算法竞赛代码模板(长期更新)
C++算法模板 基础算法 排序 快速排序 void quickSort(int q[], int l, int r) { if (l >= r) return; int i = l - 1, j ...
- 官宣:Splashtop与JumpCloud合作 提供单次登录远程访问解决方案
号外! 官宣:Splashtop与JumpCloud合作 提供单次登录远程访问解决方案! 打开百度APP,查看更多高清图片 以下是一本正经的官宣新闻,我是没感情的翻译机器人,嘻嘻. Bad Robot ...
- Unity HDRP BentNormal的理解
1.通过网上冲浪了解到,BentNormal可以解决间接环境高光漏光及间接漫反射光照漏光的问题. 这里的漏光是指间接光照部分没有考虑到模型自身的遮挡关系导致的漏光. 2.可以通过SD之类的软件烘焙Be ...
- Centos6/RHEL6下恢复ext4文件系统下误删除的文件
目录 一.关于ext4文件系统 二.linux文件系统的组成(inode,block) 三.问题:为什么删除比复制快? 四.问题:当我们误删除文件后,第一件事要做什么? 五.准备测试环境 六.安装ex ...
- 使用IDEA导入MyBatis源码进行调试
一. 下载源码 GitHub地址:https://github.com/mybatis/mybatis-3 复制上面的地址执行下列命令: git clone https://github.com/my ...
- JSON转化工具的使用
概述 JSON是一种轻量化的数据传输格式,在各种场景都有运用.比如在ajax中,服务端的数据一般通过JSON字符串的格式传输给前端,前端ajax引擎自动将JSON字符串转化为JS对象(需要将ajax的 ...
- 部署Zabbix
https://blog.csdn.net/qq_57414752/article/details/125819822
- 莫烦tensorflow学习记录 (2)激励函数Activation Function
https://mofanpy.com/tutorials/machine-learning/tensorflow/intro-activation-function/ 这里的 AF 就是指的激励函数 ...