The Triangle(数塔)
来源:互联网 发布:武汉淘宝摄影 编辑:程序博客网 时间:2024/06/05 10:46
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.
InputYour 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.
OutputYour program is to write to standard output. The highest sum is written as an integer.
Sample Input5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
题目大意:
给你一个n层的三角形,从顶点开始,每次只能加左下或右下的数,请你找到此三角形从上到下相加之和最大为多少。
<span style="font-family: Arial, Helvetica, sans-serif;">//AC..【二维数组做法】.动规(从上向下推)...</span>
#include<cstdio>#include<algorithm>using namespace std;int main(){ int n,a[110][110]; while (scanf("%d",&n)!=EOF) { int dp[110][110]={0}; for (int i=0;i<n;i++) for (int j=0;j<i+1;j++) scanf("%d",&a[i][j]); for (int i=0;i<n;i++) { for (int j=0;j<i+1;j++) { if (i==0) dp[i][j]=a[i][j]; else if (j==0) dp[i][j]=a[i][j]+dp[i-1][j]; else if (i==j) dp[i][j]=a[i][j]+dp[i-1][j-1]; else dp[i][j]=a[i][j]+max(dp[i-1][j],dp[i-1][j-1]); } }// for (int i=0;i<n;i++)// {// for (int j=0;j<i+1;j++)// {// printf("%-d ",dp[i][j]);// }// printf("\n");// } int maxsum=0; for (int i=0;i<n;i++) { if (maxsum<dp[n-1][i]) maxsum=dp[n-1][i]; } printf("%d\n",maxsum); } return 0;}【图解】
原图:
i\j 0 1 2 3 4
0 7
1 3 8
2 8 1 0
3 2 7 4 4
4 4 5 2 6 5
结果:
i\j 0 1 2 3 4
0 7
1 10 15
2 18 16 15
3 20 25 20 19
4 24 30 27 26 24
//AC...【二维数组做法】动规(从下向上推)...#include<cstdio>#include<algorithm>using namespace std;int main(){ int n,a[110][110]; while (scanf("%d",&n)!=EOF) { for (int i=0;i<n;i++) for (int j=0;j<i+1;j++) scanf("%d",&a[i][j]); int dp[110][110]={0}; for (int i=n-1;i>=0;i--) { for (int j=i;j>=0;j--) { if (i==n-1) dp[i][j]=a[i][j]; else dp[i][j]=a[i][j]+max(dp[i+1][j],dp[i+1][j+1]); } }// for (int i=0;i<n;i++)// {// for (int j=0;j<i+1;j++)// {// printf("%-d ",dp[i][j]);// }// printf("\n");// } printf("%d\n",dp[0][0]); } return 0;}【图解】
原图:
i\j 0 1 2 3 4
0 7
1 3 8
2 8 1 0
3 2 7 4 4
4 4 5 2 6 5
结果:
i\j 0 1 2 3 4
0 30
1 23 21
2 20 13 10
3 7 12 10 10
4 4 5 2 6 5
嗯,其实这道题还不算完,虽然这道题的时间复杂度已经没法优化了,不过空间复杂度却还可以优化,可以用一个一维数组的dp替换二维数组的dp。
//【一维数组做法】:优化了空间复杂度。#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[110];int a[110][110],n;int main(){ while (scanf("%d",&n)!=EOF) { for (int i=0;i<n;i++) for (int j=0;j<i+1;j++) scanf("%d",&a[i][j]); memset(dp,0,sizeof(dp)); for (int i=n-1;i>=0;i--) { //j的值要从小到大,因为后面要用到dp[j+1]的值,不能再使用前覆盖掉 for (int j=0;j<=i;j++) { if (i==n-1) dp[j]=a[i][j]; else dp[j]=a[i][j]+max(dp[j+1],dp[j]);// printf("%d ",dp[j]); }// printf("\n"); } printf("%d\n",dp[0]); } return 0;}
- The Triangle(数塔)
- nyoj 18 The Triangle(数塔)
- POJ 1163 The Triangle (数塔)
- POJ1163--The Triangle数塔
- HLG 1004 The Triangle(数塔问题DP)
- NYOJ 题目18 The Triangle(数塔)
- POJ 1163 The Triangle(DP 数塔问题)
- POJ 1163 The Triangle(数塔模型dp)
- POJ The Triangle (dp数塔)
- NYOJ-The Triangle【数塔问题】
- POJ 3176-Cow Bowling/POJ 1163-The Triangle(简单DP-数塔)
- POJ1163 HDOJ2084 THE TRIANGLE 数塔 DP启蒙
- HDU 2084 数塔+NYOJ 18 The Triangle
- The Triangle--动态规划经典问题--数塔问题
- POJ 1163 The Triangle数塔 动态规划
- The Triangle(记搜)
- (acm) The Triangle
- HDU2084 数塔 poj1163The Triangle
- 打印一棵二叉树,要求从上往下打印,每一层从左往右打印
- Android基础总结.doc(第五节、多线程下载、activity声明周期、意图操作)
- ubuntu上安装docker-compose遇到Permission denied问题
- <DATA-轉載>【数据科学】教你成为数据科学“大咖”!
- linux之scp命令
- The Triangle(数塔)
- Java IO(十):Buffered 和 Data
- 数据结构实验之栈六:下一较大值(二)
- Ansi、Unicode与通用类型
- Tomcat启动超时报错对应方法
- linux下安装软件包的方法
- poj 2109 Power of Cryptography
- Java IO(十一):序列化与 ObjectInputStream、ObjectOutputStream
- 集训训练赛day2