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

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
题目大意:求出三角形的最大和,规定路径从第一行第一个为起点,路径为向下一行的该列或者下一行的右边的一列(在所有数字向左对齐的情况下),终点为最后一行的某个数。
解题思路:这是一道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 题解的更多相关文章

  1. ZOJ 4081 Little Sub and Pascal's Triangle 题解

    ZOJ 4081 Little Sub and Pascal's Triangle 题解 题意 求杨辉三角第n行(从1开始计数)有几个奇数. 考察的其实是杨辉--帕斯卡三角的性质,或者说Gould's ...

  2. POJ1163 The Triangle 【DP】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 De ...

  3. 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 ...

  4. codechef Sums in a Triangle题解

    Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...

  5. (数字三角形)POJ1163 The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59698   Accepted: 35792 De ...

  6. POJ1163 The Triangle

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44997   Accepted: 27174 Description 73 ...

  7. 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 ...

  8. CF336A Vasily the Bear and Triangle 题解

    Content 一个矩形的顶点为 \((0,0)\),其对顶点为 \((x,y)\),现过 \((x,y)\) 作直线,分别交 \(x\) 轴和 \(y\) 轴于 \(A,B\) 两点,使得 \(\t ...

  9. 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 ...

  10. POJ1163 The Triangle: 倒三角形问题

    经典的DP问题,DP思想也很直接: 直接贴代码: #include<iostream> #include<cstdio> #include<algorithm> # ...

随机推荐

  1. dotnet OpenXML 读取 PPT 内嵌 xlsx 格式 Excel 表格的信息

    在 Office 中,可以在 PPT 里面插入表格,插入表格有好多不同的方法,对应 OpenXML 文档存储的更多不同的方式.本文来介绍如何读取 PPT 内嵌 xlsx 格式的表格的方法 读取方法和 ...

  2. Apache Pulsar 桌面端图形化管理工具

    Apache Pulsar 桌面端图形化管理工具 Apache Pulsar 是 Apache 软件基金会顶级项目,是下一代云原生分布式消息流平台,集消息.存储.轻量化函数式计算为一体,采用计算与存储 ...

  3. 一文搞懂Maven配置,从此不再糊涂下载依赖(文末有成品)

    一般来说Maven都是配合着idea一起使用,下载依赖速度慢就去网上找个镜像配置一下,但总会遇到莫名其妙的问题,比如镜像源不生效.Error reading file pom.xml等等.今天详细讲解 ...

  4. Docker部署Scrapy-redis分布式爬虫框架(整合Selenium+Headless Chrome网页渲染)

    前言 我的京东价格监控网站需要不间断爬取京东商品页面,爬虫模块我采用了Scrapy+selenium+Headless Chrome的方式进行商品信息的采集. 由于最近爬虫用的服务器到期,需要换到新服 ...

  5. SpringMVC学习三(静态资源/AJAX功能/乱码问题)

    静态资源的映射 Springmvc完成ajax功能 SpringMVC返回中文到ajax乱码问题解决方式 1.静态资源映射 对于之前web.xml配置文件中的 先做出如下更改,不可写"/*& ...

  6. SQL Server实战五:存储过程与触发器

      本文介绍基于Microsoft SQL Server软件,实现数据库存储过程与触发器的创建.执行.修改与删除等操作. 目录 1 交互式创建并执行--存储过程一 2 交互式创建并执行--存储过程二 ...

  7. WEB服务与NGINX(5)- root和alias的区别详解

    root和alias的区别 root:指定站点家目录,给定的路径对应于location中的/uri 左侧的/,文件的绝对路径为root+location. 支持环境:http, server, loc ...

  8. ubuntu配置nginx+php开发环境(virtualbox)

    A.安装环境 1.安装php7.0 sudo apt install php7.0 2.安装php7.0-fpm,这是php和nginx之间的连接器. sudo apt install php7.0- ...

  9. Splashtop 支持对 Adobe Creative Cloud 视频应用程序的远程访问

    快讯:Splashtop 为 Adobe Creative Cloud 视频和音频产品的用户提供了对其工作站的高性能远程访问,使他们能够在家中工作. 程访问和远程支持解决方案的领导者 Splashto ...

  10. 【漏洞复现】CVE-2023-27372 RCE漏洞

    产品介绍 SPIP是一个互联网发布系统,其中非常重视协作工作,多语言环境和Web作者的易用性.它是自由软件,在GNU/GPL许可证下分发.这意味着它可以用于任何互联网站点,无论是个人的还是机构的,非营 ...