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. PHP 无限级分类树

    1. function generateTree($items){    $tree = array();    foreach($items as $item){        if(isset($ ...

  2. printf函数用法小记

    By francis_hao    Aug 26,2017   C语言中printf函数是一个比较常用的函数,但是常用并不代表完全了解,本文翻译了printf的man手册,介绍了其全部功能(不包括ma ...

  3. 细谈select函数(C语言)

    Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect.accept.recv或recvfrom这样的阻塞程序( ...

  4. Qt error ------ 出现Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly

    出现原因: 使用了不存在的对象 数组越界了 用 delete 释放未分配的内存空间,或者超过一次释放同个内存 比如: 顺序不能颠倒 正确: ui->setupUi(this); ui->t ...

  5. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  6. sun.security.x509.CertAndKeyGen;找不到

    导入已有项目编译时出错,报: import sun.security.x509.CertAndKeyGen;找不到 而这个包属于sun公司的jar包.不是项目本身的问题,而是开发环境的问题. 最后原因 ...

  7. mybatis中association和collection的column传入多个参数值

    在使用 association和collection 进行关联查询的时候 column 参数可能会有多个,如下: 注意: parameterType 一定要是 java.util.Map

  8. 51Nod 1004 n^n末尾数字 | 快速幂

    #include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...

  9. (转)梯度方向直方图HOG(Histograms of Oriented Gradients )

    HOG(Histograms of Oriented Gradients )梯度方向直方图 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视 ...

  10. Linux进程调度与源码分析(一)——简介

    本系列文章主要是近期针对Linux进程调度源码进行阅读与分析后的经验总结,分析过程中可能结合部分Linux网络编程的相关知识以便于理解,加深对Linux进程调度的理解和知识分享. 本系列文章主要结合L ...