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

单纯的递归, 但是会超时
#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 110
#define max(a,b) (a>b?a:b) int a[N][N]; int DFS(int x, int y, int n)
{
if(x>n || y>n)
return ; if(x==n && y==n)
return a[x][y]; return a[x][y]+ max(DFS(x+,y, n), DFS(x+, y+, n));
} int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, j; memset(a, , sizeof(a)); for(i=; i<=n; i++)
for(j=; j<=i; j++)
scanf("%d", &a[i][j]); printf("%d\n", DFS(,,n));
}
return ;
}

用上记忆化搜索后, 不会超时了

#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 110
#define max(a,b) (a>b?a:b) int a[N][N], dp[N][N]; int DFS(int x, int y, int n)
{
if(x>n || y>n)
return ; if(dp[x][y]!=-)
return dp[x][y];
else
{
if(x==n && y==n)
return a[x][y]; dp[x+][y] = DFS(x+, y, n);
dp[x+][y+] = DFS(x+, y+, n); return a[x][y]+ max(dp[x+][y], dp[x+][y+]);
}
} int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, j; memset(a, , sizeof(a));
memset(dp, -, sizeof(dp)); for(i=; i<=n; i++)
for(j=; j<=i; j++)
scanf("%d", &a[i][j]); printf("%d\n", DFS(, , n));
}
return ;
}

(记忆化搜索 )The Triangle--hdu --1163的更多相关文章

  1. HDU 1176 免费馅饼(记忆化搜索)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  3. HDU 1428 漫步校园(记忆化搜索,BFS, DFS)

    漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...

  4. HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...

  5. [HDU 1428]--漫步校园(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1428 漫步校园 Time Limit: 2000/1000 MS (Java/Others)    M ...

  6. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

  7. HDU 4960 Another OCD Patient(记忆化搜索)

    HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...

  8. 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...

  9. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

随机推荐

  1. webpack接上一篇

    html-webpack-plugin 自动生成html文件 安装:npm install html-webpack-plugin --save-dev 使用 在webpack.config.js中引 ...

  2. JAVA课堂练习-动手动脑--数组

    1.阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 源代码: public class PassArray { public stat ...

  3. myeclipse的安装与配置和JUnit的简单使用

    安装配置 首先根据自己电脑系统选择合适的JDK版本 http://www.oracle.com/technetwork/java/javase/downloads/index.html 这是JDK下载 ...

  4. sql条件查询-日期比较(取年月部分)

    查询当年当月的数据: select * from compalete_task where to_Char(create_date,'yyyyMM') = to_Char(sysdate,'yyyyM ...

  5. socket.io的connect连接时不断的进行自动连接,并产生错误net::ERR_EMPTY_RESPONSE

    socket = io.connect('http://192.168.1.200:9043?uuid=333'); 执行上面的语句时,产生下面的错误: 后来经过排查,是由于项目的jdk版本过低引起的 ...

  6. Java环境编写

    首先安装jdk,本系统中jdk安装在D:\jdk:jre安装在D:\Jre: 然后开始配置环境变量: JAVA_HOME:D:\jdk; JRE_HOME:D:\jre; CLASSPATH:.;%J ...

  7. 【搜索】 Find The Multiple

    #include<stdio.h> #include<stdlib.h> #include<string.h> bool found; void DFS(unsig ...

  8. @Transactional注解使用心得

    配置基于注解的声明式事务: ...配置tx,aop的命名空间 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:a ...

  9. 兼容ie透明书写

    filter:alpha(opacity=0); opacity:0;filter:alpha(opacity=70); opacity:0.7;

  10. 22.上传app一些相关问题

    1.截取上传的各个屏幕尺寸 1.按最大尺寸截取,快捷键 command+s 2.在模拟器上截取 3. 截图 iphone4 : 640x960 或者 960x640 phone5    640 x 1 ...