(记忆化搜索 )The Triangle--hdu --1163
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
Output
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的更多相关文章
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 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 ...
- HDU 1428 漫步校园(记忆化搜索,BFS, DFS)
漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...
- HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...
- [HDU 1428]--漫步校园(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1428 漫步校园 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...
- HDU 4960 Another OCD Patient(记忆化搜索)
HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...
- 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
随机推荐
- Git 安装和使用教程(更加详细)
转载至:https://www.cnblogs.com/smuxiaolei/p/7484678.html#undefined Git 安装和使用教程 git 提交 全部文件 git add . g ...
- django ORM 增删改查 模糊查询 字段类型 及参数等
ORM 相关 #sql中的表 #创建表: CREATE TABLE employee( id INT PRIMARY KEY auto_increment , name VARCHAR (), gen ...
- MongoDb进阶实践之八 MongoDB的聚合初探
一.引言 好久没有写东西了,MongoDB系列的文章也丢下好长时间了.今天终于有时间了,就写了一篇有关聚合的文章.一说到“聚合”,用过关系型数据库的人都应该知道它是一个什么东西.关系型数据库有“聚合” ...
- c#devexpres TreeList 最简单显示动态值的应用
为了让数据显示在行内,也为熟练一下devexpress treelist 控件, 查找了很多,最多的是先把数据放在datatable 表里边, 然后赋值给treelist的datasource 的 ...
- 优化myeclipse启动速度以及解决内存不足问题
解决myeclipse内存不足问题: 使用 MyEclipse 开发项目后,随着项目文件的增多,以及运行时间的增加,实际上 MyEclipse 所消耗的内存是会一直增大的,有的时候会出现 MyEcli ...
- Win10传递优化设置技巧
什么是“传递优化缓存” “传递优化”是微软为了加快Windows更新和Microsoft Store应用更新的下载速度,而在Windows10中引入的一种“自组织分布式本地化缓存”设计,可以在用户电脑 ...
- MySQL学习笔记-数据库后台线程
数据库后台线程 默认情况下讲述的InnoDB存储引擎,以后不再重复声明.后台线程有7个--4个IO thread,1个master thread,1个锁监控线程,1个错误监控线程.IO thread的 ...
- laravel错误1071 Specified key was too long; max key length is 1000 bytes
Laravel 5.5 环境,php artisan migrate 之后,出现错误如题. 检查了一下,代码是这样的: $table->increments('id'); $table-> ...
- Linux修改/etc/profile配置错误command is not found自救方法
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- JDK8集合类源码解析 - HashSet
HashSet 特点:不允许放入重复元素 查看源码,发现HashSet是基于HashMap来实现的,对HashMap做了一次“封装”. private transient HashMap<E,O ...