poj 1163-小白算法练习 The Triangle 动态规划

The Triangle
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 49986 Accepted: 30197

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

翻译

 输入一个三角形数列,计算最大的路径值,你从定点开始算,只能向左下和向右下移动,路径值是你走的路上的数字之和

 思路

我这里是写成最小值了,最大值也一样的。 

作为小白的我们根本连什么是动态规划,什么分治啊什么的都不知道,其实这些就是个名字,根本不重要,我们要的是思路,这才是学算法最重要的。那我们一开始的想法是什么?把整个三角形所有的路径值都算一遍,那我们来看看需要多少种情况,第一行一个数,求一次,第二行一个数,1*1次,第三行二个数,1*1*2次,那我们最大的数据是100行就是99!次咯。那我们知道计算机每秒处理速度以百万条数据来衡量,99!大约是多少呢?9.3326215443944 * 10 155,计算机算这个称程序要用好几分钟,那么这个思路是不正确的。我们再想!我们看上面的sample input,你肉眼是怎么算的,你肯定不会全算一遍吧,你是找5-7-3(你不会取找8,那我们抛弃了一些数据了)-1-2-4

是不是第一次就会这样去找数据,为什么我们直接这样找数据,而不去算,那你这么快算出来是不是抛弃了许多计算量,那么编程时也是这样的。

7                 7              A    7
3 8         -->   3  8          -->   10 15          -->那么接下来你会怎么算是觉得10比12小所以12以后 
8 1 0             8  1  0             8  1  0            的数据不用算了,还是仍要往下比较这个数据?
2 7 4 4           2  7  4  4          2  7  4  4
4 5 2 6 5         4  5  2  6  5       4  5  2  6  5 

7                 7              B
3 8         -->   3  8          -->   10              -->这种方法会出问题,因为你只考虑了当前的最大利益
8 1 0             8  1  0             8  1  0             没有考虑整体的最佳利益,也就是说每个当前的最大
2 7 4 4           2  7  4  4          2  7  4  4           利益可能获取不到整体的当前利益,虽然说这种方
4 5 2 6 5         4  5  2  6  5       4  5  2  6  5         法速度更快。

选取A路线

						--既然选择了暂时把21留下,那下面一行怎么求呢
7                 7              A    7
3 8         -->   3  8          -->   10 15          --> 10  15
8 1 0             8  1  0             8  1  0            18  11  15  -->是这样的吧-->然后接这往下求 
2 7 4 4           2  7  4  4          2  7  4  4         13  18  15  19
4 5 2 6 5         4  5  2  6  5       4  5  2  6  5      17  18  17  21  24  -->排序
或者从底下开始,更好,只能向上或者左上

7                 17               
3 8         -->   10 14             
8 1 0             14 7  6              
2 7 4 4           6  9  6  9           
4 5 2 6 5         4  5  2  6  5       

代码

#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	int before[100][100];
	int middle[100][100];
	int N;
	cin>>N;//三角形行数 
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<=i;j++)
		{
			cin>>before[i][j];
		}	
	}	
	for(int i=N-1;i>=0;i--)
	{
		for(int j=0;j<=i;j++)
		{
			if(i==N-1)
			{
				middle[i][j]=before[i][j];	
			} 	
			else
			{
				middle[i][j]=min(middle[i+1][j]+before[i][j],middle[i+1][j+1]+before[i][j]);
			}
		}	
	}	
	cout<<middle[0][0]<<endl;
	return 0;
} 
------- 求最大值把min改成max就好了  -------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值