POJ 1579-Function Run Fun(内存搜索)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 16503 | Accepted: 8514 |
Description
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
Output
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
巨水。。
一開始wa了一组数据 (0,21,21)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 116
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,m,dp[22][22][22];
int dfs(int x,int y,int z)
{
if(x<=0||y<=0||z<=0)
return 1;
if(x>20||y>20||z>20)
return dfs(20,20,20);
if(dp[x][y][z]!=INF)
return dp[x][y][z];
dp[x][y][z]=0;
if(x<y&&y<z)
dp[x][y][z]+=(dfs(x,y,z-1)+dfs(x,y-1,z-1)-dfs(x,y-1,z));
else dp[x][y][z]+=(dfs(x-1,y,z)+dfs(x-1,y-1,z)+dfs(x-1,y,z-1)-dfs(x-1,y-1,z-1));
return dp[x][y][z];
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c))
{
if(a==-1&&b==-1&&c==-1)break;
memset(dp,INF,sizeof(dp));
printf("w(%d, %d, %d) = %d\n",a,b,c,dfs(a,b,c));
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
POJ 1579-Function Run Fun(内存搜索)的更多相关文章
- POJ 1579 Function Run Fun 【记忆化搜索入门】
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
- poj 1579 Function Run Fun(记忆化搜索+dp)
题目链接:http://poj.org/problem?id=1579 思路分析:题目给出递归公式,使用动态规划的记忆搜索即可解决. 代码如下: #include <stdio.h> #i ...
- POJ 1579 Function Run Fun
简单动态规划,详细代码网上有!
- poj 1579 Function Run Fun 【记忆化递归】
<题目链接> 题目大意: 给出一些递归式,直接套用这些递归式计算. 解题分析: 递归式已经由题目明确说明了,但是无脑递归铁定超时,所以此时,我们需要加上记忆化,对于那些已经算过的,就没有必 ...
- POJ 1579 Function Run Fun 记忆化递归
典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用 ...
- POJ 1597 Function Run Fun
记忆化搜索. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- poj 1579(动态规划初探之记忆化搜索)
Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17843 Accepted: 9112 ...
- 洛谷P1464 Function HDU P1579 Function Run Fun
洛谷P1464 Function HDU P1579 Function Run Fun 题目描述 对于一个递归函数w(a,b,c) 如果a≤0 or b≤0 or c≤0就返回值11. 如果a> ...
- HDU 1331 Function Run Fun(记忆化搜索)
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
随机推荐
- 【Android开发经验】来,咱们自己写一个Android的IOC框架!
到眼下位置.afinal开发框架也是用了好几个月了,还记得第一次使用凝视完毕控件的初始化和事件绑定的时候,当时的心情是多么的兴奋- -代码居然能够这样写!然后随着不断的学习,也慢慢的对IOC框架和注解 ...
- jQuery语音播放插件
自己做jQuery插件:将audio5js封装成jQuery语音播放插件 日前的一个项目需要用到语音播放功能.发现Audio5js符合需求且使用简单,又鉴于jQuery控件便于开发操作,于是有了以 ...
- D - Cow Ski Area
Description Farmer John's cousin, Farmer Ron, who lives in the mountains of Colorado, has recently t ...
- 开发并调试 Mail Add-in
开发并调试 Mail Add-in (mail app for Outlook) 准备工作 如果你的邮箱搭建在 Exchange Server 上,则可以创建邮件应用程序(Mail Add-in)来扩 ...
- Codeforces 443 B. Kolya and Tandem Repeat
纯粹练JAVA.... B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megab ...
- JavaScript学习之获取URL参数
最近看了几道面试题,其中有一道就是关于写一个方法来获取浏览器地址栏URL查询部分的字段内容.虽然之前看过相关的东西,但感觉有点模糊,所以就又全面的学习一遍,谨以此文记之! 准备知识 在JavaScri ...
- Jquery Jqprint—随着Jquery Jqprint实现网页打印
研究关于利用空闲时间今天Jquery Jqprint插入,用这个Jquery脚本就可以实现轻松打印指定的页面内容功能区: 样品A: <!DOCTYPE html PUBLIC "-// ...
- WebApi路由及版本控制
public class WebApiControllerSelector : IHttpControllerSelector { private const string NamespaceKey ...
- Java有用的经验--Swing片
Java有用经验总结--Swing篇 前言 本文前言部分为我的一些感想,假设你仅仅对本文介绍的Java有用技巧感兴趣,能够跳过前言直接看正文的内容. 本文的写作动机来源于近期接给人家帮忙写的一个小程序 ...
- BIP Requests Are Failing With Error "OPP Error Oracle.apps.xdo.XDOException: Error Creating Lock Fil
In this Document Symptoms Cause _afrLoop=975833031487795&id=1512691.1&displayIndex=1&a ...