题目传送门:http://poj.org/problem?id=1579

Function Run Fun

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20560   Accepted: 10325

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

题意概括:

要求写一个函数 w( a, b, c) 处理输入数据(多测试);

①如果 a < 0 || b < 0 || c < 0;直接返回w( a, b, c );

②如果 a > 20 || b > 20 || c > 20;返回w( 20, 20, 20 );

③如果 a < b && b < c ;返回 w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c);

④ 其他情况返回w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1) ;

解题思路:

记忆化搜索裸题。

AC code:

 /// POJ 1579 记忆化搜索入门
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define ll long long int
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = ;
int d[MAXN][MAXN][MAXN]; int dfs(int a, int b, int c)
{
if(a <= || b <= || c <= ) return ;
if(a > || b > || c > ) return dfs(, , );
if(d[a][b][c]) return d[a][b][c];
if(a < b && b < c) d[a][b][c] = dfs(a, b, c-)+dfs(a, b-, c-) - dfs(a, b-, c);
else d[a][b][c] = dfs(a-, b, c)+dfs(a-, b-, c)+dfs(a-, b, c-)-dfs(a-,b-,c-);
return d[a][b][c];
}
int main()
{
int ans, A, B, C;
memset(d, , sizeof(d));
while(~scanf("%d%d%d", &A, &B, &C))
{
if(A == - && B == - && C == -) break;
ans = dfs(A, B, C);
printf("w(%d, %d, %d) = %d\n", A, B, C, ans);
}
return ;
}

POJ 1579 Function Run Fun 【记忆化搜索入门】的更多相关文章

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

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

  2. POJ 1579 Function Run Fun 记忆化递归

    典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用 ...

  3. poj 1579(动态规划初探之记忆化搜索)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. python 获取子目录下的所有文件的路径

    import os pathss=[] for root, dirs, files in os.walk(tarpath): path = [os.path.join(root, name) for ...

  2. OSG DB的插件地址设置

    今天搞了一整天OSG,结果每次都说could not find plugin,就是说找不到OSG的插件去加载文件,我大概看了下OSG的插件机制,发现他是用插件的形式下去读取文件的 http://blo ...

  3. 微博关系服务与Redis的故事

    http://www.infoq.com/cn/articles/weibo-relation-service-with-redis?utm_source=articles_about_Redis&a ...

  4. python 模板注入

    今天学习了python的模板注入,这里自己搭建环境测试以下,参考文章:http://www.freebuf.com/articles/web/136118.html web 程序包括两个文件: fla ...

  5. Node.js 框架对比之 Express VS Koa

    背景 上图是一个典型的采用 Node.js 开发 web 应用的前后端结构,下面介绍一下 Node 服务层在其中的作用以及使用 Node.js 的一些优劣. Node 服务层作用: 请求代理 传统做法 ...

  6. C#基础(第一天)

    Ctrl+K+D:对其代码: #Region      #endRegion:折叠多余代码: Ctrl+K+S:可以折叠代码写注释: 语法格式:数据类型  变量名:                  ...

  7. Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)

    Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性) 前言:由于前段时间忙于写接口,在接口中需要做很多的参数校验,本着简洁.高效的原则,便写了这个小工具供自己使用(内容 ...

  8. Nginx反向代理、负载均衡功能

    环境: [root@db02 ~]# uname -a Linux db02 -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 x86_64 GNU/Li ...

  9. SpringSecurity 3.2入门(10)自定义权限控制认证及授权的过程

    上一章的代码实现还存在一些问题,如角色表.权限表的用处没有体现出来,但是已经能完成URL拦截功能,后面将会继续完善认证及授权的过程. 认证及授权的过程如下: 1.容器启动,MyInvocationSe ...

  10. struts2返回结果类型

    在action下还有result标签 1.result不只有name,其实还有type result返回类型在struts-default.xml默认的配置文件中有定义,可以看到有result-typ ...