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. 浅谈CSS3 box-reflect 属性

    今天说一下 CSS3的box-reflect属性,也就是倒影特效. 语法: box-reflect:包括3个值. 1. direction 定义方向,取值包括 above . below . left ...

  2. php bin/console doctrine:migrations:migrate

    $ php bin/console doctrine:migrations:migrate                    XinXiBundleMigrationsWARNING! You a ...

  3. Spark-GraphxAPI学习笔记

    图的集合视图 graph包含三个基本的类集合视图: val vertices: VertexRDD[VD] val edges: EdgeRDD[ED] val triplets: RDD[EdgeT ...

  4. 觉得VR头显太笨重?轻便的VR“神器”来了

    一直以来需要搭配手机才能使用的VRBOX(VR眼镜盒子)都被大家诟病携带不便.比较笨重.不透气等等问题.大家也一直期待能够有轻便的搭配手机的VR设备出现,最好是可以随身携带的.另外一方面,作为手机最常 ...

  5. trove 开发者阅读翻译

    介绍 Trove为OpenStack提供数据库的服务.它的设计运行完全符合OpenStack,目标是让用户能快速.轻松地利用关系数据库的特点,没有负担的处理复杂的管理任务.云用户和数据库管理员可以根据 ...

  6. 舒适的路线 (code[vs] 1001)

    传送门 :code[vs]  1001 思路:拿到这题的首先的思路 , 就是跑一遍最短路. 可是在尝试了一会后发现不会写,于是果断弃 尝试另外的算法.. 于是就有的以下的算法.并查集 + 乱搞(有点像 ...

  7. hdu 1848 Fibonacci again and again(简单sg)

    Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2 ...

  8. 转:Spark User Defined Aggregate Function (UDAF) using Java

    Sometimes the aggregate functions provided by Spark are not adequate, so Spark has a provision of ac ...

  9. 相机标定 matlab opencv ROS三种方法标定步骤(3)

    三 ,  ROS 环境下 如何进行相机标定 刚开始做到的时候遇到一些问题没有记录下来,现在回头写的时候都是没有错误的结果了,首先使用ROS标定相机, 要知道如何查看节点之间的流程图  rosrun r ...

  10. IOS 基于TCP的socket通信详解(原创)

    最近在整理通信层相关知识,这篇文章是边整理边写的,有些地方可能不够准确,还请各位路过的大牛专家指出来.这次整理的socket通信是基于TCP的,实现方式是GCD形式,以下记录的都是些理论知识,方便自己 ...