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. SQL注入(四)

    参数绑定(预编译语句) 虽然数据库自带的过滤是个不错的实现,但是我们还是处在“用户输入被当成 SQL语句的一部分 ”这么个圈子里,其实要跳出这个圈子还有一个实现,就是参数绑定.基本上所有的主流数据库都 ...

  2. IntelliJ IDEA新建JAVA WEB项目(转载)

    IntelliJ IDEA是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本控制工具(git.svn ...

  3. Javascript赋值语句中的“&&”操作符和"||"操作符

    有这么一种常见的语句: var a = a || 4; 那赋值语句中的"&&"操作符和"||"操作符是什么意思?如何知道这两个逻辑操作符两旁的数 ...

  4. centos tomcat 安装

    安装说明 安装环境:CentOS-6.3 安装方式:源码安装  软件:apache-tomcat-7.0.29.tar.gz 下载地址:http://tomcat.apache.org/downloa ...

  5. AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转

    http://blog.csdn.net/zhangh8627/article/details/51752872 AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖 标签:  ...

  6. chrome ipc 网摘

    http://my.oschina.net/myspaceNUAA/blog/81632?p=1 http://lihuan623.blog.163.com/blog/static/138595845 ...

  7. CHD 2015迎新杯题解

    A.预防流感的拉面女神 简析:计算 n 的二进制表示里面 1 的个数 #include <cstdio> #include <cstring> #include <alg ...

  8. delphi 输入文件相对路径的更改,更改成用户的

  9. Uva 11889 Benefit (lcm与gcd)

    题意:给你两个数,a,c,求出 lcm(a,b)==c 时的 b 的最小值 思路:我们知道一个性质 gcd(a,b)*lcm(a,b) = a*b 由此我们可以得到 b = gcd(a,b)*lcm( ...

  10. table中td内容过长 省略号显示

    首先设置 css样式: table { table-layout: fixed;} HTML中的table代码: <tr> <th class="col-md-1" ...