记忆化搜索 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个格子,允许上下左右走,每次走的格子 ...
随机推荐
- php 制作略缩图
一.需求 最近公司的项目中有个需求,就是用户上传自己的微信二维码,然后系统会自动将用户的微信二维码合并到产品中 二.分析 因为该系统是手机端的,所以从用户端的体验出发,用户当然是直接在微信上保存二维码 ...
- 自己整理的HTML基本标签参考知识
基 本 标 签 创建一个HTML文档 <html></html> 设置文档标题以及其他不在WEB网页上显示的信息 <head></head> ...
- 利用set特性判断list是否存在重复的值
List<String> list2=new ArrayList();//存放很多值的list //根据set不能存储相同的值该特性来判断list2中的值是否重复 HashSet set ...
- js 如何给标签增加属性
<html> <head> <meta charset="UTF-8"> <title></title> </he ...
- R 连接mysql数据库
一.配置RODBC 1.R下载RODBC包,安装好.2.在http://dev.mysql.com/downloads/connector/odbc下载mySQL ODBC,安装好.3.windows ...
- js 习题
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- js消息框
<script> function del(obj, id) { layer.confirm('是否要删除信息!', { btn: ['确定', '取消'] }, function (in ...
- SSL证书在线申请和取回证书指南
1.客服下单后用户会收到一封邮件,来验证接收证书的邮箱;如图1所示:只有完成此邮箱验证才能正常申请证书; 请注意:为了确保您或您的用户能正常收到WoSign证书管理系统自动发送的邮件,请一定要把系统发 ...
- Ubuntu安装RTX2080显卡驱动
安装RTX2080显卡驱动 近日新购了一台DELL服务器,用于TensorFlow,由于显卡是另加的,需要安装显卡驱动. 服务器配置 服务器型号:DELL PowerEdge R730 CPU:2*I ...
- C#第七节课
for嵌套 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...