题目传送门: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. API 接口收集

    节假日 http://api.goseek.cn/ http://timor.tech/api/holiday http://www.easybots.cn/api/holiday.php?d=201 ...

  2. JavaScript设计模式(二) - 单例模式

    什么是单例模式? 单例模式从字面上的理解是不困难的,js上就是指只有一个对象实例. 为什么需要单例模式? 我们可以将一些成员变量封装在一个单例对象中,每次访问这些变量都只能从这个单例对象进行访问,这样 ...

  3. TOJ 4523 Transportation

    Description Given N stations, you want to carry goods from station 1 to station N. Among these stati ...

  4. [转].NET Core之Entity Framework Core 你如何创建 DbContext

    本文转自:http://www.cnblogs.com/tdws/p/5874212.html 本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明博客园蜗牛原文地址 http://www. ...

  5. 日期控件html

    日期控件多的是,这里收录的是最简单的一种 <html> <head> <script type="text/javascript"> funct ...

  6. Mysql数据库死锁分析相关概念

    参考博客: mysql死锁问题分析(https://www.cnblogs.com/LBSer/p/5183300.html) mysql insert锁机制(http://yeshaoting.cn ...

  7. web应用开发周期

    web应用开发周期 1. 前期准备 2. 编码 3. 上线 4. 数据分析 5. 持续交付 6. 遗留系统 7. 回顾与新架构 重构的一般性因素 1. 系统难以维护 2. 系统技术栈难以符合业务需求 ...

  8. Eclipse SWT

    Reference: http://www.eclipse.org/swt/ http://www.functionx.com/win32/Lesson01.htm http://www.win32d ...

  9. IT集中监控

    监控的从底层到上应该是: 一 数据采集层 二 数据处理层 三 数据展示层 监控需要和ITIL中定义的服务进行相当多的交互,例如监控会使用配置管理数据库CMDB来记录和读取数据,会将事件处理方式从知识库 ...

  10. spring---FactoryBean与BeanFactory的区别

    1.BeanFactory BeanFactory是IOC最基本的容器,负责生产和管理bean,它为其他具体的IOC容器提供了最基本的规范,例如DefaultListableBeanFactory, ...