Function Run Fun
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17843   Accepted: 9112

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

记忆化搜索。照着思路走。不过把已经搜索过的就不要重复计算了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
using namespace std; int f[][][];
int dfs(int a,int b,int c){
if(a<=||b<=||c<=) return ;
if(a>||b>||c>) return dfs(,,);
if(f[a][b][c]!=-) return f[a][b][c];
if(a<b&&b<c) f[a][b][c] = dfs(a,b,c-)+dfs(a,b-,c-)-
dfs(a,b-,c);
else f[a][b][c]= dfs(a-,b,c)+dfs(a-,b-,c)+dfs(a-,b,c-)
-dfs(a-,b-,c-);
return f[a][b][c];
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
if(a==-&&b==-&&c==-) break;
memset(f,-,sizeof(f));
int ans = dfs(a,b,c);
printf("w(%d, %d, %d) = %d\n",a,b,c,ans);
}
return ;
}

poj 1579(动态规划初探之记忆化搜索)的更多相关文章

  1. poj 1088 动态规划+dfs(记忆化搜索)

    滑雪 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Description Mi ...

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

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

  3. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

  4. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. poj 1661 Help Jimmy(记忆化搜索)

    题目链接:http://poj.org/problem?id=1661 一道还可以的记忆化搜索题,主要是要想到如何设dp,记忆化搜索是避免递归过程中的重复求值,所以要得到dp必须知道如何递归 由于这是 ...

  6. poj 1085 Triangle War 博弈论+记忆化搜索

    思路:总共有18条边,9个三角形. 极大极小化搜索+剪枝比较慢,所以用记忆化搜索!! 用state存放当前的加边后的状态,并判断是否构成三角形,找出最优解. 代码如下: #include<ios ...

  7. 【动态规划】【记忆化搜索】hdu5965 扫雷

    f(i,j,k)表示第i行,放的雷的状态为j{0表示不放,1表示往上放,2表示往下放,3表示上下都放},剩余还有k(0<=k<=2)个要放的方案数. 先给出我这个sb写的错误代码,死都没调 ...

  8. POJ 3616 Milking Time ——(记忆化搜索)

    第一眼看是线段交集问题,感觉不会= =.然后发现n是1000,那好像可以n^2建图再做.一想到这里,突然醒悟,直接记忆化搜索就好了啊..太蠢了.. 代码如下: #include <stdio.h ...

  9. POJ 1661 Help Jimmy ——(记忆化搜索)

    典型的记忆化搜索问题,dfs一遍即可.但是不知道WA在哪里了= =,一直都没找出错误.因为思路是很简单的,肯定是哪里写挫了,因此不再继续追究了. WA的代码如下,希望日后有一天能找出错误= =: —— ...

随机推荐

  1. 从零开始学Linux系统(一)之引导流程解析

    Linux系统:分时多用户多任务的操作系统: Linux系统引导流程: inittab配置文件中: 定义了linux系统的运行的7个级别:从0~6 0.6:分别代表关机和重启,不建议设置为默认的运行级 ...

  2. 添加网站标题logo

    如何在标题栏title前添加网站logo? 第一种方法:据说在网站根目录下放着我们的ico型logo,命名为favicon.ico,浏览器会自动去找到并显示.试了试,在firefox23和ie8下都没 ...

  3. Java日期时间实用工具类

    Java日期时间实用工具类 1.Date (java.util.Date)    Date();        以当前时间构造一个Date对象    Date(long);        构造函数   ...

  4. [nginx]nginx rewrite规则之last和break

    c俺靠这篇博文 http://eyesmore.iteye.com/blog/1142162 有用的配置: 1.开启rewrite_log,这样在/var/log/nginx/error.log中显示 ...

  5. Inner join case when

    SELECT ( ), wn.ActualWorkflowNumber) + ' ' + wi.SN ) AS SN , wi.RecordID , wi.WorkflowName , wc.Work ...

  6. 10.nginx+ffmpeg上搭建HLS切片

    1.首先介绍一下HLS协议: (1)简介 这个协议是由苹果公司提出并推广使用的,维基百科介绍如下: HTTP Live Streaming(缩写是HLS)是一个由苹果公司提出的基于HTTP的流媒体网络 ...

  7. jenkins修改job默认名字

    ${GIT_BRANCH,fullName="false"}-${BUILD_NUMBER}-${GIT_REVISION,length=12}-${deploy_rollback ...

  8. 「6月雅礼集训 2017 Day4」暴力大神hxx

    [题目大意] 给出一个n重循环,每重循环有范围$[l, r]$,其中$l$,$r$可能是之前的变量,也可能是常数.求循环最底层被执行了多少次. 其中,保证每个循环的$l$,$r$最多有一个是之前的变量 ...

  9. codeforces——contest 864 problemE

    Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable it ...

  10. 【vijos】P1659 河蟹王国

    [算法]线段树 [题解]区间加上同一个数+区间查询最大值.注意和谐值可以是负数,初始化ans为负无穷大. #include<cstdio> #include<algorithm> ...