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. Excel上传找到错误数据类型

    一:查询数据库表中字段的类型语句 SELECT CASE WHEN col.colorder = 1 THEN obj.name ELSE '' END AS 表名, col.colorder AS ...

  2. Vue 插件和Preset

    插件和Preset 插件 Vue CLI 使用了一套基于插件的架构 Vue CLI 使用了一套基于插件的架构.如果你查阅一个新创建项目的 package.json,就会发现依赖都是以 @vue/cli ...

  3. Liunx cal

    1.命令格式: cal [参数][月份][年份] 2.命令功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份 3.命令参数: -1 显示一个月的 ...

  4. 引用类型中的push()、pop()、shift()方法

    /** * write by waitingbar1014 * time 2013.10.18 * 用途:常用于如一堆游戏激活码中获得抽取的值 * **/ //有以下一个数组: var arrayli ...

  5. static ,const

    函数原型作用域:作用范围为函数形参表范围,起始于“(”,结束于“)”,这是声明 定义是属于局部作用域,所以声明的时候不写形参名字也可以 类作用域:包含类体及在类外实现的本类成员函数的函数体 静态生存期 ...

  6. egg 为企业级框架和应用而生, 阿里出品

    https://eggjs.org/zh-cn/intro/ egg 是什么? egg 为企业级框架和应用而生,我们希望由 egg 孕育出更多上层框架,帮助开发团队和开发人员降低开发和维护成本. 设计 ...

  7. Luogu 3690 LCT - 模板

    推荐几篇比较好的博客: FlashHu 的 讲解比较好 : 传送门 Candy 的 代码~ : 传送门 以及神犇Angel_Kitty的 学习笔记: 传送门 Code V 模板 #include< ...

  8. $(QTDIR);$(QTDIR)\include\QtCore;$(QTDIR)\include;

    $(QTDIR); 在系统环境变量中定义即可  vs属性中设置头文件路径

  9. 服务程序 -st

    Windows 服务由三部分组成:1.一个服务可执行文件:2.一个服务控制程序(SCP):3.服务控制管理器(SCM),负责在 HKLM\SYSTEM\CurrentControlSet\Servic ...

  10. Java的反射技术

    什么是反射机制 Java的反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能调用它的任意属性和方法.这种动态获取信息以及动态调用对象属性和方法的即使称为J ...