(记忆化搜索 )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 ...
随机推荐
- mysql 常用 sql
查询表创建时间.修改时间等 SELECT * FROM information_schema.tables WHERE table_schema='ty_supplier' AND table_ ...
- dev NavBarControl控件
一.新建一个导航栏 拖入一个panel到窗口上做为导航栏的容器,然后再拖入一个NavBarControl到其上,点击NavBarControl控件的右上角三角箭头展开任务列表,选择PaintStyle ...
- MySQL表的相关操作
操作数据表之前,必须先选择相应数据表所在的数据库 mysql> USE databaseName; -- 选择数据库 查看该数据库下的数据表 mysql> show tables; 确定数 ...
- Carbon document
< Getting Started Docs Reference History Contribute Github Introduction The Carbon class is inh ...
- [SoapUI] Groovy获取HTTP Status
def value = messageExchange.responseHeaders["#status#"] def httpResponseHeaders = context. ...
- 买茶叶想到的哪个比较便宜 x1/y1 >x2/y2 x代表多少钱 y代表 多少克 无聊的试炼
茶叶1 128元 200克 茶叶2 330元 160克 当然这个哪个便宜 一眼就知道了,这里不过抛砖引玉 128元 330元 200克 160克 我们把价钱用x表示 多少克 ...
- oracle导出expdp导入impdp
conn sys/password as sysdba;创建用户test1CREATE USER test1 IDENTIFIED BY "pass1";GRANT CONNECT ...
- SpringMVC学习八 @ResponseBody注解
(一)在方法上只有@RequestMapping 时,无论方法返回值是什么认为需要跳转,代码实例如下 @RequestMapping("demo10") public People ...
- 几张图片帮助记忆docker基本原理(转)
写的非常好的一篇文章,不知道为什么被删除了. 利用Google快照,做个存档. 快照地址:地址 作者地址:青牛 什么是dockerDocker 是一个开源项目,诞生于 2013 年初,最初是 dotC ...
- keras框架的MLP手写数字识别MNIST,梳理?
keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...