Problem 1004 Number Triangle

Accept: 2230    Submit: 5895
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be 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.

          7
       3   8
     8  1  0
   2  7  4  4
 4  5  2   6  5
 

In the sample above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30.

 Input

There are multiple test cases.The first line of each test case contains R (1 <= R <= 1000), the number of rows. Each subsequent line contains the integers for that particular row of the triangle. All the supplied integers are non-negative and no larger than 100.

 Output

Print a single line containing the largest sum using the traversal specified for each test case.

 Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

 Sample Output

30

很明显的一道动归题,类似于求最大路径,方法有很多,搜索啊,递归啊,,,但用动归是最简单的,动归思路有两种:

1:

用二维数组存放数字三角形;

D( r, j) : 第r行第 j 个数字(r,j从1 开始算)

MaxSum(r, j) : 从D(r,j)到底边的各条路径中最佳路径的数字之和,问题是就求MaxSum(1,1);
典型的递归问题
D(r, j)出发,下一步只能走D(r+1,j)或者D(r+1, j+1)。故对于N行的三角形:
if ( r == N)
MaxSum(r,j) = D(r,j)
else
MaxSum( r, j) = Max{ MaxSum(r+1,j), MaxSum(r+1,j+1) } + D(r,j);

2: 如果每算出一个MaxSum(r,j)就保存起来,下次用到其值的时候直接取用,则可免去重复计算。

那么可以用 O(n2)时间完成计算。因为三角形的数字总数是 n(n+1)/2;这时,递归转化为递推;

下面呈现两种思路代码:

1:超时代码:

#include <iostream>
#include <algorithm>
#define MAX 101
using namespace std;
int D[MAX][MAX];
int n;
int MaxSum(int i, int j){
if(i==n)
return D[i][j];
int x = MaxSum(i+,j);
int y = MaxSum(i+,j+);
return max(x,y)+D[i][j];
}
int main(){
int i,j;
cin >> n;
for(i=;i<=n;i++)
for(j=;j<=i;j++)
cin >> D[i][j];
cout << MaxSum(,) << endl;
}
如果采用递规的方法,深度遍历每条路径,存在大量重复计算。则时间复杂度为 2n, 对于 n = 100行,肯定超时。

  2:记忆递归

#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 101
int D[MAX][MAX]; int n;
int maxSum[MAX][MAX];
int MaxSum(int i, int j){
if( maxSum[i][j] != - )
return maxSum[i][j];
if(i==n) maxSum[i][j] = D[i][j];
else {
int x = MaxSum(i+,j);
int y = MaxSum(i+,j+);
maxSum[i][j] = max(x,y)+ D[i][j];
}
return maxSum[i][j];
}
int main(){
int i,j;
cin >> n;
for(i=;i<=n;i++)
for(j=;j<=i;j++) {
cin >> D[i][j];
maxSum[i][j] = -;
}
cout << MaxSum(,) << endl;
}

3:

#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 101
int D[MAX][MAX]; int n;
int maxSum[MAX][MAX];
int main() {
int i,j;
cin >> n;
for(i=;i<=n;i++)
for(j=;j<=i;j++)
cin >> D[i][j];
for( int i = ;i <= n; ++ i )
maxSum[n][i] = D[n][i];
for( int i = n-; i>= ; --i )
for( int j = ; j <= i; ++j )
maxSum[i][j] = max(maxSum[i+][j],maxSum[i+][j+]) + D[i][j]
cout << maxSum[][] << endl;
}
人人为我”递推型动归程序

4:当然了,3中的代码也可以空间优化,从下往上找

#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 101
int D[MAX][MAX];
int n; int * maxSum;
int main(){
int i,j;
cin >> n;
for(i=;i<=n;i++)
for(j=;j<=i;j++)
cin >> D[i][j];
maxSum = D[n]; //maxSum指向第n行
for( int i = n-; i>= ; --i )
for( int j = ; j <= i; ++j )
maxSum[j] = max(maxSum[j],maxSum[j+]) + D[i][j];
cout << maxSum[] << endl;
}
如果看不懂简单手推模拟一遍你就明白了,不难。。和3中代码类似,只是将二维转化为一维了;

FZU1004-Number Triangle经典动归题,核心思路及代码优化的更多相关文章

  1. 6581 Number Triangle

    6581 Number Triangle 时间限制:500MS  内存限制:1000K提交次数:57 通过次数:47 题型: 编程题   语言: G++;GCC Description 7 3 8 8 ...

  2. JAVA经典算法40题及解答

    JAVA经典算法40题 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分 ...

  3. JAVA经典算法40题

    1: JAVA经典算法40题 2: [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 3 ...

  4. JAVA经典算法40题(原题+分析)之分析

    JAVA经典算法40题(下) [程序1]   有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   1.程序分析:  ...

  5. JAVA经典算法50题(转)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51097928 JAVA经典算法50题 [程序1]   题目:古典问题:有一对兔子, ...

  6. JAVA经典算法40题面向过程

    JAVA经典算法40题 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分 ...

  7. java经典算法40题-附带解决代码

    前一段时间工作比较闲,每天没有代码敲的日子有点无聊,于是为了保证自己的编程逻辑力的日常清醒,故百度了一些经典的java算法,然后自己思考编程解决问题,虽然那些东西比较基础了,但是有些题目小编看到了也是 ...

  8. UVA 674 Coin Change 换硬币 经典dp入门题

    题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数. 经典的dp题...可以递推也可以记忆化搜索... 我个人比较喜欢记忆化搜索,递推不是很熟练. 记忆化搜索:很白 ...

  9. JAVA经典算法40题(原题+分析)之原题

    JAVA经典算法40题(上) [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? [程 ...

随机推荐

  1. mysql学习之通过文件创建数据库以及添加数据

    转自:http://blog.163.com/wujicaiguai@126/blog/static/170171558201411311547655/ 1.# 创建数据库语句 create data ...

  2. 521 Longest Uncommon Subsequence I 最长特殊序列 Ⅰ

    给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列).子序列可以通过删去字符串中的某些字符实现,但不能改变剩余 ...

  3. redis过期事件

    背景:目前在商城项目,订单有过期逻辑,小伙伴提议用redis做,经讨论分析,redis有key的过期事件,貌似可以实现,但是咨询大神,好像不建议这样用,可能会丢数据 随便写了段python代码测试 i ...

  4. spring事务问题

    springmvc中在service层中有如下逻辑:1.提交事务2.开启新线程,新线程中的业务依赖1中提交的事务处理办法:在service中新建一个方法do,调本地提交事务的方法doTranction ...

  5. Spring-bean(零)

    内容提要:红为1,黄2,绿3 -----配置形式:基于xml文件的方式:基于注解的方式 -----Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),Facto ...

  6. poj1857 To Europe! To Europe!

    思路: 一维dp. 实现: #include <cstdio> #include <iostream> using namespace std; const int INF = ...

  7. JDBC的fetchsize和maxrows

    在我们的项目开发中,可能有把SQL查询的结果保存到CSV然后提供下载的功能.当查询的结果集相当大的时候,很容易报内存不足错误(outofmemory).那该怎么解决这种情况的内存不足错误呢? 其实在J ...

  8. sqlserver:查询锁住sql以及解锁

    --查看被锁表:SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableNameFROM s ...

  9. makefile vpath变量

    在讲vpath之前,我们首先了解以下makefile文件. 在类Unix系统中,当我们使用源码编译某个软件的时候,我们会使用confiure,make,make install这三个命令,其中cofi ...

  10. dmesg -检测和控制内核环缓冲

    NAME dmesg - print or control the kernel ring buffer 总览 dmesg [ -c ] [ -n 级别 ] [ -s 缓冲区大小 ] 描述 dmesg ...