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 ...
随机推荐
- 基于jquery ui修改的不依赖第三方的背景透明的弹出div
效果图: 代码: <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- oracle卸载清除注册表(彻底卸载)
用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,那么怎么才能完全卸载Oracle呢? 那就是直接注册表清除,步骤如下: . 开始->设置->控制面板-& ...
- 混合用法模式 __name__和__main__
# -*- coding: utf-8 -*- #python 27 #xiaodeng #混合用法模式 __name__和__main__ #可把文件作为模块导入,并以独立式程序的形式运行,每个模块 ...
- 深入了解MyBatis参数
参考1 参考2 参考3
- springmvc ModelAndView 和 Model
@RequestMapping("") public ModelAndView index(HttpSession session) { Object data = session ...
- Visual C#两分钟搭建BHO IE钩子
微软在1997年正式推出Browser Helper Object (BHO), 使程序员能够更好的对IE进行二次开发和操作. 在通过编写BHO程序数月后, 我希望把我的一些经验告诉才开始的同志, 避 ...
- Windows右键菜单设置与应用技巧
http://blog.163.com/lee_honleon/blog/static/555251522008014258896/ 需要修改注册表,挺麻烦的.最好还是把迅雷卸了重装,不会影响什么 ...
- 工作总结 EntityFramework中出现DateTime2异常的完美解决办法
EntityFramework中出现DateTime2异常的完美解决办法 今天在使用entityframework往数据库插入数据的时候,突然出现了一个数据类型转换异常的问题: System.Da ...
- http请求No peer certificate的解决方法
不少同学在做HTTP请求新浪授权或新浪数据的时候会出现 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate的异常.现给出解决方法 ...
- [android] 网络断开的监听
标签: android 网络监听 杂谈 分类: Android [转自] http://blog.sina.com.cn/s/blog_5d5996d001012o07.html 其实手机在网络方面 ...