POJ 1163 The Triangle 解题报告
2007-05-30 00:49
489 查看
算法1:递归
设 f(i, j) 是从(i, j)点出发向下走的最长路径。
f(i, j) = max( f(i+1, j), f(i+1, j+1) ).
输出f(0, 0).
code:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX 101
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int triangle[MAX][MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int longestPath(int i,int j);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main(){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int i,j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cin >> n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=0;i<n;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=0;j<=i;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cin>> triangle[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cout<<longestPath(0,0) <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int longestPath(int i,int j){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if(i==n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int x =longestPath(i+1,j);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int y =longestPath(i+1,j+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if(x<y)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
x=y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return x+triangle[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
超时!!!
原因:大量重复计算
f(0, 0) = max( f(1, 0), f(1, 1) ).
f(1, 0) = max( f(2, 0), f(2, 1) ).
f(1, 1) = max( f(2, 1), f(2, 2) ).
f(2, 0) = max( f(3, 0), f(3, 1) ).
f(2, 1) = max( f(3, 1), f(3, 2) ).
f(2, 2) = max( f(3, 1), f(3, 2) ).
。
。
。
f(4, 0) = max( f(5, 0), f(5, 1) ).
。
。
算法2:动态规划
一般的转化方法:
原来递归函数有几个参数,就定义一个几维的数组,数组的下标是递归函数参数的取值范围,数组元素的值是递归函数的返回值,这样就可以从边界开始,逐步填充数组,相当于计算递归函数值的逆过程。
code:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include<stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int triangle[MAX][MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int n, i, j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
scanf("%d", &n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=0; i<n; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=0; j<=i; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
scanf("%d", triangle[i]+j);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=n-2; i>=0; i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=0; j<=i; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
triangle[i][j] += triangle[i+1][j] > triangle[i+1][j+1] ? triangle[i+1][j] : triangle[i+1][j+1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
printf("%d/n", triangle[0][0]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
设 f(i, j) 是从(i, j)点出发向下走的最长路径。
f(i, j) = max( f(i+1, j), f(i+1, j+1) ).
输出f(0, 0).
code:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX 101
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int triangle[MAX][MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int longestPath(int i,int j);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main(){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int i,j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cin >> n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=0;i<n;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=0;j<=i;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cin>> triangle[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cout<<longestPath(0,0) <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int longestPath(int i,int j){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if(i==n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int x =longestPath(i+1,j);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int y =longestPath(i+1,j+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if(x<y)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
x=y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return x+triangle[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
超时!!!
原因:大量重复计算
f(0, 0) = max( f(1, 0), f(1, 1) ).
f(1, 0) = max( f(2, 0), f(2, 1) ).
f(1, 1) = max( f(2, 1), f(2, 2) ).
f(2, 0) = max( f(3, 0), f(3, 1) ).
f(2, 1) = max( f(3, 1), f(3, 2) ).
f(2, 2) = max( f(3, 1), f(3, 2) ).
。
。
。
f(4, 0) = max( f(5, 0), f(5, 1) ).
。
。
算法2:动态规划
一般的转化方法:
原来递归函数有几个参数,就定义一个几维的数组,数组的下标是递归函数参数的取值范围,数组元素的值是递归函数的返回值,这样就可以从边界开始,逐步填充数组,相当于计算递归函数值的逆过程。
code:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include<stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int triangle[MAX][MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int n, i, j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
scanf("%d", &n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=0; i<n; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=0; j<=i; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
scanf("%d", triangle[i]+j);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=n-2; i>=0; i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=0; j<=i; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
triangle[i][j] += triangle[i+1][j] > triangle[i+1][j+1] ? triangle[i+1][j] : triangle[i+1][j+1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
printf("%d/n", triangle[0][0]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
相关文章推荐
- 【原】 POJ 1163 The Triangle 三角形最大路径 动态规划 解题报告
- (转)POJ 1163 The Triangle 解题报告
- POJ 1163 The Triangle 解题报告
- POJ 1163 解题报告
- POJ 1163解题报告
- poj 1163 The Triangle之解题报告
- poj 1163解题报告
- POJ-1988-Cube Stacking 解题报告
- POJ 1012 Joseph 解题报告
- POJ 2246 Matrix Chain Multiplication 解题报告(栈)
- poj 2138-TOYS解题报告
- poj 1113-Walls解题报告
- POJ 2977 生理周期 解题报告
- poj 3104-Drying解题报告
- POJ 1144 Tarjan 割点 解题报告
- Poj1163 The Triangle(动态规划求最大权值的路径)
- POJ_1007:DNA Sorting解题报告
- POJ 1953 解题报告
- POJ - 1416 Shredding Company解题报告(dfs)
- POJ 1948 Triangular Pastures 解题报告