记忆化搜索 hdu 1331
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.
1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1
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
能够通过空间换时间的方法。将计算出的值
//考查知识点:记忆化搜索 就是用数组存储。降低递归函数调用的次数和时间
//考试当天做出来了。今天又做一遍,居然有点生疏了,(⊙﹏⊙)b #include<stdio.h>
int s[22][22][22];
void f()
{
int i,j,k;
for(i=1;i<22;++i)
{
for(j=1;j<22;++j)
{
for(k=1;k<22;++k)
{
if(i<j&&j<k)
{
if(k-1==0)
s[i][j][k-1]=s[i][j-1][k-1]=1;
if(j-1==0)
s[i][j-1][k-1]=s[i][j-1][k]=1;
s[i][j][k]=s[i][j][k-1]+s[i][j-1][k-1]-s[i][j-1][k];
continue;
}
if(i==1)
s[i-1][j][k]=s[i-1][j-1][k]=s[i-1][j][k-1]=s[i-1][j-1][k-1]=1;
if(j==1)
s[i-1][j-1][k]=s[i-1][j-1][k-1]=1;
if(k==1)
s[i-1][j][k-1]=s[i-1][j-1][k-1]=1;
s[i][j][k]=s[i-1][j][k]+s[i-1][j-1][k]+s[i-1][j][k-1]-s[i-1][j-1][k-1];
}
}
}
}
int main()
{
int a,b,c;
f();
while(~scanf("%d%d%d",&a,&b,&c),!(a==-1&&b==-1&&c==-1))
{
if(a<=0||b<=0||c<=0)
{
printf("w(%d, %d, %d) = 1\n",a,b,c);
continue;
}
if(a>20||b>20||c>20)
{
printf("w(%d, %d, %d) = %d\n",a,b,c,s[20][20][20]);
continue;
}
printf("w(%d, %d, %d) = %d\n",a,b,c,s[a][b][c]);
}
return 0;
}
记忆化搜索 hdu 1331的更多相关文章
- HDU 1331 Function Run Fun(记忆化搜索)
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
- HDU 1176 免费馅饼(记忆化搜索)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1428 漫步校园(记忆化搜索,BFS, DFS)
漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...
- HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...
- [HDU 1428]--漫步校园(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1428 漫步校园 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...
- HDU 4960 Another OCD Patient(记忆化搜索)
HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...
- 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...
随机推荐
- 在redhat6上装1.8以下的docker
因为目前1.8以上的docker最低要求是3.10的Linux内核,而我的内核版本远低于此. [root@localhost home]# uname -r -.el6.x86_64 鉴于我的vm上有 ...
- 题解报告:hdu 1848 Fibonacci again and again(尼姆博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...
- WebApi中对请求参数和响应内容进行URL编码解码
项目经测试,发现从IE提交的数据,汉字会变成乱码,实验了网上很多网友说的给ajax加上contentType:"application/x-www-form-urlencoded; char ...
- Redis 链表结构 和 常用命令
Redis 数据结构 --链表(linked-list) 命令 说明 备注 lpush key node1 [node2 ...] 把节点 node1 加入到 链表最左边 如果是 node1.node ...
- Android 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi
Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: Installation failed with message Faile ...
- Linux通信之异步通知模式
[参考]韦东山 教学笔记 为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:1. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID. 不过此项工 ...
- Java多线程中常见的几个问题
我们都知道,在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 1.进程和线程的区别是什么? 进程是执行着的应用程序,而线程是进程内部的一个执行序列. ...
- C#:winform项目在win7,xp32位和64位都能运行
vs中项目配置管理器活动解决方案平台选择X86平台.
- CMMI评估流程
原文链接:http://www.cmmcn.com/new/cmmi-105.html 当前位置:首页 >> CMMI知识库 >> CMMI相关 >> CMMI评估 ...
- Python学习①. 基础语法
Python 简介 Python 是一种解释型,面向对象的语言.特点是语法简单,可跨平台 Python 基础语法 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编 ...