POJ1163 The Triangle
来源:互联网 发布:网络云盘 编辑:程序博客网 时间:2024/05/15 05:35
1.题目信息(http://poj.org/problem?id=1163)
The Triangle
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 30397 Accepted: 17973
Description
73 88 1 02 7 4 44 5 2 6 5(Figure 1)
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
573 88 1 0 2 7 4 44 5 2 6 5
Sample Output
30
2.算法分析
本题是经典的动态规划题,要求出要求的最大值(最优)。大致意思是,从三角形顶部数字走,每次只能走到这个数字的左下角或者右下角的数字,直到底部,计算走过的线路的数字之和,求这个和的最大值,我们暂且称之为数字的最大路线和。我们可以转换为求任意数字的最大路线和,本题要求的最大值即:所有数字的最大路线和的最大值。比如,我们要求数字1的最大路线和,则最大路线是: 7->8->1,最大路线和就是16 。我们把三角记录在数组a中,把数字的最大路线和数组m中,则动态规划方程如下:
m[ i ][ j ] = max { m[ i -1][ j-1 ] + m[ i-1][ j ] } + a[ i ][ j ]
即:某一数字的最大路线和 = max {左上角数字最大路线和,右上角数字最大路线和} + 该数字值
3.参考代码
/* Name: POJ1163 The Triangle Author:chenbin Date: 09-10-12 12:00 Description: http://poj.org/problem?id=1163*/
#include <stdio.h>#include <string.h>int main(){ short int a[100][100],i = 0,j = 0,N,max = 0,upleft = 0,upright = 0; memset(a,0,sizeof(a)); scanf("%d",&N); for(i = 0;i < N ; i++) { for(j = 0 ; j <= i ; j++) { scanf("%d",&a[i][j]); short upleft = (i-1 >= 0)&&(j-1 >= 0) ? a[i-1][j-1] : 0; short upright = (i-1 >= 0) ? a[i-1][j] : 0; a[i][j] = ( upleft > upright ? upleft : upright )+ a[i][j]; max = a[i][j] > max ? a[i][j] : max; } } printf("%d",max); return 0;}
4.代码优化
可从如下几个方面优化代码
(1).数组采用压缩矩阵,可以节省一半内存
(2).将i=0,j=0的特殊情况特殊处理,可消除upleft和upright的的判断,甚至可以不用这两个变量。
(3).定义宏,替换?:求最大值的方式。
如有其它好的算法和代码,欢迎讨论
- POJ1163 The Triangle
- POJ1163:The Triangle
- POJ1163 The Triangle
- POJ1163 The Triangle 【DP】
- POJ1163 The Triangle
- POJ1163 The Triangle
- POJ1163 The Triangle
- POJ1163 The Triangle
- poj1163 the triangle 题解
- POJ1163 The Triangle
- POJ1163 The Triangle 题解
- POJ1163 The Triangle
- poj1163 dp the triangle
- POJ1163 The Triangle
- poj1163 The Triangle
- poj1163 The Triangle
- The Triangle(poj1163)
- POJ1163 The Triangle【DFS】
- 鼠标是否会从大众的手中消失
- 敏捷个人周刊下载-2012年10月份第一期
- 使用LoadRunner运行场景时的监控问题
- “结巴”分词:做最好的Python分词组件
- JVM执行篇:使用HSDIS插件分析JVM代码执行细节
- POJ1163 The Triangle
- oracle常规用法
- 一篇基于opencv的人脸识别文章
- Android Matrix理论与应用详解
- Struts2整合liferay6时使用struts2的标签报错
- Java 非阻塞算法详解
- Java内部类
- 秋天给孩子饮水四技巧
- linux下禁止用户使用密码方式登陆,而使用密钥方式登陆