HDU 1331 Function Run Fun(记忆化搜索)
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
#include<stdio.h>
int w(int a,int b,int c)
{
if(a<=||b<=||c<=)return ;
else if(a>||b>||c>)return w(,,);
else if(a<b&&b<c)return w(a,b,c-)+w(a,b-,c-)-w(a,b-,c);
else return w(a-,b,c)+w(a-,b-,c)+w(a-,b,c-)-w(a-,b-,c-);
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
if(a==-&&b==-&&c==-)break;
printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
}
}
运行结果是这样的。
然后我发现在递归的过程中有很多重复的部分,导致超时,所以要用记忆化搜索来解决。
也就是把已经计算出来的结果放在一个数组里保存,下次计算到这里的时候直接读取结果就可以了。
下面是正确代码。
#include<stdio.h>
int dp[+][+][+]={};
int w(int a,int b,int c)
{
if(a<=||b<=||c<=)return ;
if(dp[a][b][c]!=)return dp[a][b][c];//直接读取
else if(a<b&&b<c)
{
dp[a][b][c]=w(a,b,c-)+w(a,b-,c-)-w(a,b-,c);
return dp[a][b][c];
}
else
{
dp[a][b][c]= w(a-,b,c)+w(a-,b-,c)+w(a-,b,c-)-w(a-,b-,c-);
return dp[a][b][c];
}
}
int main()
{
int a,b,c,res;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
if(a==-&&b==-&&c==-)break;
if(a<=||b<=||c<=)res=;
else if(a>||b>||c>)res=w(,,);
else res=w(a,b,c);
printf("w(%d, %d, %d) = %d\n",a,b,c,res);
}
}
HDU 1331 Function Run Fun(记忆化搜索)的更多相关文章
- poj 1579 Function Run Fun(记忆化搜索+dp)
题目链接:http://poj.org/problem?id=1579 思路分析:题目给出递归公式,使用动态规划的记忆搜索即可解决. 代码如下: #include <stdio.h> #i ...
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1428 漫步校园(记忆化搜索,BFS, DFS)
漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- hdu 1142(迪杰斯特拉+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 1176免费馅饼(记忆化搜索)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 题意不解释了 简单的记忆化搜索可以拿来练练手,注意要从pos = 5 开始搜索 #include ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU 2476 String painter(记忆化搜索, DP)
题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...
随机推荐
- 运行ORB-SLAM笔记_使用篇(二)
1. 编译完成之后就可以使用了,按照说明我们可以知道,首先开启roscore
- Linux下网卡BCM4313的安装
我遇到的问题:打开网络管理->wifi 显示固件缺失 通过: 1 lspci | grep Wireless 显示: 1 08:00.0 Network controller: Broadcom ...
- ubuntu安装jdk的两种方法
方法一: 这种方法比较简单,保证虚拟机网络畅通就可以了 sudo apt-get update sudo apt-get install default-jre sudo apt-get instal ...
- soa服务治理-dubbo
dubbo官网:http://dubbo.io/Home-zh.htm 学习点: 1. 日志的配置
- 自己通过centos6.5配置NFS 成功后的笔记,希望对需要的人有点点帮助吧!
环境介绍: 服务器:centos 172.16.250.170 客户端:centos 172.16.250.172 先用rpm -qa ...
- MATLAB符号极限、导数及级数求和
作者:长沙理工大学 交通运输工程学院 王航臣 1.函数的极限 函数:limit 功能:求取函数的极限 语法: limit(f) limit(f,x,a) limit(f,x,a,'right') li ...
- gmic全球移动互联网大会 全球九站已开启!
由长城会主办的全球移动互联网大会( 简称GMIC)已成长为世界范围内最具影响力的辐射并连结东西半球的移动互联网商务平台,是最大规模的移动互联网行业盛会. 2017gmic全球移动互联网大会北京站将于2 ...
- google 技巧
inurl: 用于搜索网页上包含的URL. 这个语法对寻找网页上的搜索,帮助之类的很有用. intext: 只搜索网页部分中包含的文字(也就是忽略了标题,URL等的文字). site: 可以限制你搜索 ...
- (从零开始java开发) IDEA+MAVEN构建一个webapp骨架项目(解决一直downloading问题)
折腾了一段时间终于解决了, 可能是因为网络问题 xml一直没法访问 maven 骨架生成项目速度慢的令人发指,都在Generating project in Batch mode等待,Idea状态显示 ...
- MPMoviePlayerController
属性 说明 @property (nonatomic, copy) NSURL *contentURL 播放媒体URL,这个URL可以是本地路径,也可以是网络路径 @property (nonatom ...