Function Run Fun-递归+细节处理
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
#include<stdio.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std; int book[][][];
int w(int a,int b,int c)
{
if(a<=||b<=||c<=)
return ;
else if(a>||b>||c>)
return w(,,);
else if(book[a][b][c]!=-)
return book[a][b][c];
else if(a<b&&b<c)
return book[a][b][c]=w(a,b,c-)+w(a,b-,c-)-w(a,b-,c);
else
return book[a][b][c]=w(a-,b,c)+w(a-,b-,c)+w(a-,b,c-)-w(a-,b-,c-);
// return book[a][b][c];
}
int main()
{
int a,b,c;
while(~scanf("%d %d %d",&a,&b,&c))
{
if(a==-&&b==-&&c==-)
break;
memset(book,-,sizeof(book));
printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
}
return ;
}
int w(int a,int b,int c)
{
if(a>=&&a<=&&b>=&&b<=&&c>=&&c<=&&book[a][b][c]!=-)//防止越界
return book[a][b][c];
if(a<=||b<=||c<=)
return ;
else if(a>||b>||c>)
return w(,,);
else if(a<b&&b<c)
return book[a][b][c]=w(a,b,c-)+w(a,b-,c-)-w(a,b-,c);
else
return book[a][b][c]=w(a-,b,c)+w(a-,b-,c)+w(a-,b,c-)-w(a-,b-,c-);
}
Function Run Fun-递归+细节处理的更多相关文章
- POJ 1579 Function Run Fun 【记忆化搜索入门】
		
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
 - 洛谷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> ...
 - poj 1579 Function Run Fun 【记忆化递归】
		
<题目链接> 题目大意: 给出一些递归式,直接套用这些递归式计算. 解题分析: 递归式已经由题目明确说明了,但是无脑递归铁定超时,所以此时,我们需要加上记忆化,对于那些已经算过的,就没有必 ...
 - POJ 1579 Function Run Fun 记忆化递归
		
典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用 ...
 - HDU 1331 Function Run Fun(记忆化搜索)
		
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
 - POJ1579:Function Run Fun
		
Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c ...
 - hdu1579 Function Run Fun(深搜+记忆化)
		
版权声明:本文为博主原创文章.未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37076755 转载请注明出处 ...
 - hdu 1331 Function Run Fun
		
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
 - ural 1353. Milliard Vasya's Function(背包/递归深搜)
		
1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...
 
随机推荐
- tarjan强连通缩点——cf711D
			
模板题 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 500005 #define ...
 - Bootstrap 避免模态框在用户点击背景空白处时,会自动关闭。
			
问题: Bootstrap 模态框在用户点击背景空白处时,会自动关闭. 解决方法: 在HTML页面中编写模态框时,在div初始化时添加属性 aria-hidden=”true” data-backdr ...
 - python Map()和reduce()函数
			
Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...
 - python使用threading获取线程函数返回值的实现方法
			
python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...
 - 为了避免hexo博客换了电脑应该提前做的准备。
			
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 本文转自:https://www.jiansh ...
 - Airbnb React/JSX 编码规范
			
Airbnb React/JSX 编码规范 算是最合理的React/JSX编码规范之一了 内容目录 基本规范 Class vs React.createClass vs stateless 命名 声明 ...
 - 快速列出大纲.提纲.归纳知识点 思维导图工具Xmind
			
博客搬迁,给您带来的不便敬请谅解! http://www.suanliutudousi.com/2017/10/23/%E5%BF%AB%E9%80%9F%E5%88%97%E5%87%BA%E5%A ...
 - java-day23
			
事务的四大特征: 1.原子性:是不可分割的最小操作单位,要么同时成功,要么同时失败. 2.持久性:当事务提交或回滚后,数据库会持久化的保存数据. 3.隔离性:多个事务之间,相互独立. 4.一致性:事务 ...
 - html5本地存储(一)------ web Storage
			
在html5中与本地存储相关的两个相关内容:Web Storage 与本地数据库 web Storage存储机制是对html4中的cookie存储机制的一个改善.web Storage就是在web上 ...
 - zabbix--源码安装部署zabbix3.2
			
zabbix运行在lamp环境或者lnmp环境都是可以的,如果是新系统推荐使用lamp或者lnmp一键安装包, 或者可以向下面这种方式: PHP安装 源码安装 rpm -ivh php55w-comm ...