POJ1579:Function Run Fun
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
Source
#include <iostream>
#include <cstdio> using namespace std; int main()
{
int w[][][], ans, a, b, c;
//预处理
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
{
if(!i || !j || !k) w[i][j][k] = ;
else if(i < j && j < k)
w[i][j][k] = w[i][j][k-] + w[i][j-][k-] - w[i][j-][k];
//w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
else
w[i][j][k] = w[i-][j][k] + w[i-][j-][k] + w[i-][j][k-] - w[i-][j-][k-];
//w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
}
while(scanf("%d %d %d", &a, &b, &c))
{
if(a == - && b == - && c == -) break;
if(a <= || b <= || c <= ) ans = ;
else if(a > || b > || c > ) ans = w[][][];
else ans = w[a][b][c];
printf("w(%d, %d, %d) = %d\n", a, b, c, ans);
}
return ;
}
方法二:
总之要先把之前运算出来的结果存到三维数组中,避免重复运算。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int dp[][][]; int dfs(int a,int b,int c)
{
if(a<= || b<= || c<=)
return ;
if(a> || b> || c>)
return dfs(,,);
if(dp[a][b][c])
return dp[a][b][c];
if(a<b && b<c)
dp[a][b][c] = dfs(a,b,c-)+dfs(a,b-,c-)-dfs(a,b-,c);
else
dp[a][b][c] = dfs(a-,b,c)+dfs(a-,b-,c)+dfs(a-,b,c-)-dfs(a-,b-,c-);
return dp[a][b][c];
} int main()
{
int a,b,c;
memset(dp,,sizeof(dp));
while(~scanf("%d%d%d",&a,&b,&c))
{
if(a == - && b == - && c == -)
break;
printf("w(%d, %d, %d) = %d\n",a,b,c,dfs(a,b,c));
} return ;
}
POJ1579: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> ...
- hdu 1331 Function Run Fun
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
- HDU 1331 Function Run Fun(记忆化搜索)
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
- hdu1579 Function Run Fun(深搜+记忆化)
版权声明:本文为博主原创文章.未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37076755 转载请注明出处 ...
- ACM学习历程——HDU1331 Function Run Fun(锻炼多维dp的打表)
Description We all love recursion! Don't we? Consider a three-parameter recursive function w( ...
- Function Run Fun-递归+细节处理
We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a < ...
- POJ 1579 Function Run Fun
简单动态规划,详细代码网上有!
- poj 1579 Function Run Fun(记忆化搜索+dp)
题目链接:http://poj.org/problem?id=1579 思路分析:题目给出递归公式,使用动态规划的记忆搜索即可解决. 代码如下: #include <stdio.h> #i ...
随机推荐
- 指定spring中bean启动的顺序
参考链接: https://www.jb51.net/article/125846.htm 使用DependsOn Spring 中的 DependsOn 注解可以保证被依赖的bean先于当前bean ...
- python之模块csv之CSV文件一次写入多行
# -*- coding: utf-8 -*- #python 27 #xiaodeng #CSV文件一次写入多行 import csv #csv文件,是一种常用的文本格式,用以存储表格数据,很多程序 ...
- tar压缩解压
把所有jpg文件和temp文件夹压缩到 jpg.tar.gz 压缩文件中 tar -czvf jpg.tar.gz *.jpg temp 把jpg.tar.gz 解压缩当前文件中 tar -xzv ...
- rarlinux基于linux-x64
winrar在Win都吃香,在也linux也凄惨... 风萧萧兮易水寒,壮士一去兮不复还! 00.下载rarlinux wget http://www.rarlab.com/rar/rarlinux ...
- 【转载】CodeLite汉化
这几天在Ubuntu下做程序想找一个代码提示功能比较好的IDE但又不想用NETBEANS和ECLIPSE,找到CodeLite但是它是全英文的,比较晕.找了一下,下载了一个windows版的准备回去研 ...
- JS中函数的 this 各种指向
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 情形1:如果一个函数中有this,但是它没有被上一级的对象所调用, ...
- Windows下面安装和配置Solr 4.9(二)
将Solr和Tomcat结合: 1.在D盘下创建目录 D:\Demos\Solr 2.解压solr-4.9.0文件,我这里下载的是这个4.9版本,将example文件夹下的solr文件夹中的所有文件( ...
- 键盘事件keydown、keypress、keyup随笔整理总结
英文输入法: 事件触发顺序:keydown - > keypress - > keyup 中文输入法: firfox:输入触发keydown,回车确认输入触发keyup chr ...
- iOS:即时通讯之<了解篇 SocKet>
什么是socket? 计算机专业术语就是: 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进 ...
- pythonl练习笔记——threading创建进程锁Lock()
1 基本概述 采用threading.Lock()对象创建锁lock,即 lock = threading.Lock() 其相关方法主要有 lock.acquire() # lock the lock ...