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 ...
随机推荐
- Shell基础(四):字符串截取及切割、字符串初值的处理、基使用Shell数组、expect预期交互、使用正则表达式
一.字符串截取及切割 目标: 使用Shell完成各种Linux运维任务时,一旦涉及到判断.条件测试等相关操作时,往往需要对相关的命令输出进行过滤,提取出符合要求的字符串. 本案例要求熟悉字符串的常见处 ...
- DLL中使用字符串时的注意事项。
library dll1; uses SysUtils, Classes; {$R *.res} function TESTDLL:string;stdcall; begin Result:='tes ...
- TFS——更改计算机名称,影响TFS使用
今天把自己电脑的计算机名称改了,打开VS的时候,就提示以下的错误: 报错情况 显示错误:工作区 DADI--20141015Q;SD-SERVER\Administrator 未驻留在此计算机上.如果 ...
- Application.GetOpenFilename 使用说明
Application.GetOpenFilename(FileFilter, FilterIndex, Title, ButtonText, MultiSelect) 语法: 名称 ...
- node.js 中的 fs (文件)模块
记录 fs 模块的方法及使用 1. fs.stat 获取文件大小,创建时间等信息 // 引入 fs 模块 const fs = require('fs'); fs.stat('01.fs.js', ( ...
- Flask数据库的基本操作
Flask操作数据库基本操作 常用的SQLAlchemy字段类型 类型名 python中类型 说明 Integer int 普通整数,一般是32位 SmallInteger int 取值范围小的整 ...
- html-基础知识二
form 功能:向服务器传输数据,实现用户和web 服务器的交互 一.表单属性 accept-charset: 规定在提交表单中使用的字符集 action:规定向何处提交表单地址(url) autoc ...
- Web开发常规调试方法与常见问题分析
一.Web项目基本原理 现在的web项目大都已经前后端独立开发与部署. 前后端独立开发,一般是前端与后端通过web接口(常见的有RESTful与websocket)文档进行交流.前端开发人员先更具业务 ...
- D3.js(v3)+react 制作 一个带坐标与比例尺的散点图 (V3版本)
上一章做了柱形图,https://www.cnblogs.com/littleSpill/p/10835041.html 这一章做散点图. 散点图(Scatter Chart),通常是一横一竖 ...
- Center OS 7 Apache安装配置
感谢:https://blog.csdn.net/u014157384/article/details/79497761 该作者的帮助. 自己购买了国外的服务器,想把我的网页放到服务器,网页是以web ...