Problem Description
We all love recursion! Don't we?

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.

 
Input
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.
 
Output
Print the value for w(a,b,c) for each triple.
 
Sample Input
1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18-1 -1 -1
 
Sample Output
w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1
 
Source
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1074 1078 1160 1208 1355 
以下是我直接递归的错误代码。

#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(记忆化搜索)的更多相关文章

  1. poj 1579 Function Run Fun(记忆化搜索+dp)

    题目链接:http://poj.org/problem?id=1579 思路分析:题目给出递归公式,使用动态规划的记忆搜索即可解决. 代码如下: #include <stdio.h> #i ...

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

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

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

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

  4. HDU - 1078 FatMouse and Cheese (记忆化搜索)

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

  5. hdu 4111 Alice and Bob 记忆化搜索 博弈论

    Alice and Bob Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  6. hdu 1142(迪杰斯特拉+记忆化搜索)

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

  7. hdu 1176免费馅饼(记忆化搜索)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 题意不解释了 简单的记忆化搜索可以拿来练练手,注意要从pos = 5 开始搜索 #include ...

  8. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  9. HDU 2476 String painter(记忆化搜索, DP)

    题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...

随机推荐

  1. js判断获取浏览器关闭状态

    如题,js获取浏览器关闭状态,可实现判断选择是否关闭. <html> <head> <title> </title> </head> < ...

  2. [z] .net与java建立WebService再互相调用

    http://blog.csdn.net/yenange/article/details/5824967 : .net建立WebService,在Java中调用. 1.在vs中新建web 简单修改一下 ...

  3. shell-正则表达式

    证则表达式:在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或替换那些符合某个模式的文本内容.许多程序设计 ...

  4. react-gulp-browserify

    环境搭配参照 http://www.cnblogs.com/guolaomao/p/6276877.html 前半部分的内容. 首先安装browserify npm install --save-de ...

  5. Python学习笔记基础篇——总览

    Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...

  6. java list<string>集合 传递值给js的数组

    转载地址:http://blog.sina.com.cn/s/blog_611f65fd0100msc6.html. 1.Action 中代码              List result = n ...

  7. CUDA开发存储器运用(包括存储器之间的转存)

    主机端内存(host memory) 主机端叶锁定内存(pinned memory) 显存 寄存器(register) 局部存储器(local memory) 共享存储器(shared memory) ...

  8. Ddos 攻击

    title: Ddos 防御相关 tags: Ddos, 安全, 防御 grammar_cjkRuby: true --- 防御基础 1.1. 攻击流量到底多大 谈到DDoS防御,首先就是要知道到底遭 ...

  9. [Q]无矩形外框块参照图形的识别

    该图纸的图框由块参照组成,其外侧图框不是矩形 使用默认设置无法正确识别,需要做以下修改:不勾选“块/外部参照”,勾选“块/外部参照边界”,勾选“制定块”并选择图框(块参照).

  10. [河南省ACM省赛-第三届] 房间安排 (nyoj 168)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=168 分析:找到一天中需要最多的房间即可 #include<iostream> ...