poj1351Number of Locks(记忆化搜索)
题目链接:
思路:
这道题是维基百科上面的记忆化搜索的例题。。。
四维状态dp[maxn][5][2][5]分别表示第几根棒子,这根棒子的高度,是否达到题目的要求和使用不同棒子数。那么接下来就是状态转移了。。。要用到位运算推断曾经是否这样的高度的棒子用到没。。。那么这个问题就攻克了。。。
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1126 | Accepted: 551 |
Description
one pair of neighboring slots with their difference of height equal to 3 and also there are at least 3 different height values of the slots for a lock. If a batch of locks is manufactured by taking all over the 4 values for slot height and meet the two limitations
above, find the number of the locks produced.
Input
Output
Sample Input
2
3
-1
Sample Output
2: 0
3: 8
Source
#include<cstdio>
#include<cstring>
#include<iostream>
#define New (1<<(d-1))
using namespace std;
const int maxn=17+10;
long long dp[maxn][5][2][5];
int n;
long long dfs(int ith,int height,int k,int use,int s)
{
if(dp[ith][height][k][use]!=-1)
return dp[ith][height][k][use];
if(ith==n)
{
if(k&&use>=3)
return 1;
else
return 0;
}
long long ans=0;
int tmp;
for(int d=1;d<=4;d++)
{
if(!(s&New))
tmp=use+1;
else
tmp=use;
// tmp=min(use,3);
if(k||(d*height==4&&d!=2))
ans=ans+dfs(ith+1,d,1,tmp,s|New);
else
ans=ans+dfs(ith+1,d,0,tmp,s|New);
}
return dp[ith][height][k][use]=ans;
}
int main()
{
while(~scanf("%d",&n))
{
if(n==-1) return -1;
printf("%d: ",n);
memset(dp,-1,sizeof(dp));
if(n<3)
puts("0");
else
{
dfs(0,0,0,0,0);
printf("%lld\n",dp[0][0][0][0]);
}
}
return 0;
}
#include<cstring>
#include<iostream>
#define New (1<<(d-1))
using namespace std; const int maxn=17+10;
long long dp[maxn][5][2][5];
int n; long long dfs(int ith,int height,int k,int use,int s)
{
if(dp[ith][height][k][use]!=-1)
return dp[ith][height][k][use];
if(ith==n)
{
if(k&&use>=3)
return 1;
else
return 0;
}
long long ans=0;
int tmp;
for(int d=1;d<=4;d++)
{
if(!(s&New))
tmp=use+1;
else
tmp=use;
// tmp=min(use,3);
if(k||(d*height==4&&d!=2))
ans=ans+dfs(ith+1,d,1,tmp,s|New);
else
ans=ans+dfs(ith+1,d,0,tmp,s|New);
}
return dp[ith][height][k][use]=ans;
} int main()
{
while(~scanf("%d",&n))
{
if(n==-1) return -1;
printf("%d: ",n);
memset(dp,-1,sizeof(dp));
if(n<3)
puts("0");
else
{
dfs(0,0,0,0,0);
printf("%lld\n",dp[0][0][0][0]);
}
}
return 0;
}
poj1351Number of Locks(记忆化搜索)的更多相关文章
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- loj 1044(dp+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...
- DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...
- HDU1978 记忆化搜索
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- bzoj4562: [Haoi2016]食物链--记忆化搜索
这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...
- 数位dp/记忆化搜索
一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an ...
随机推荐
- 如何用纯 CSS 创作一支诱人的冰棍
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/vrxzMw 可交互视频教 ...
- 【mysql】 load local data infield 报错 ERROR 1148 (42000): The used command is not allowed with this MySQL version
mysql> load data local infile '/Users/flint/learn/mysql/pet' into table bx_pet; 执行报错 ERROR 1148 ( ...
- 解决 mounting /dev/block/mmcblk0p1 on /sdcard failed
http://www.liyu8.com/article/sdcard.htm 之前在recovery下的adb shell执行mount -a总是会有 mount: mouting /dev/blo ...
- 快照、克隆,xshell优化,Linux历史
目录 一.虚拟拍照功能 二.虚拟机克隆功能 三.Xshell的优化 四.介绍Linux历史 一.虚拟拍照功能 1.拍摄快照 关机状态拍照 关机命令:shutdown -h now 或者 init 0 ...
- PYDay10&11&12&13-常用模块:time|datetime|os|sys|pickle|json|xml|shutil|logging|paramiko、configparser、字符串格式化、py自动全局变量、生成器迭代器
1.py文件自动创建的全局变量 print(vars()) 返回值:{'__name__': '__main__', '__package__': None, '__loader__': <_f ...
- BNUOJ 19297 Code Refactoring
Code Refactoring Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Ori ...
- POJ 2106-Boolean Expressions,双栈运用类似表达式求值!
Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...
- HDU 1724 Ellipse ——Simpson积分
[题目分析] 一看题目,直接把椭圆积分起来就可以了嘛. 然后发现椭圆比较难积分,还是算了吧. 用Simpson积分硬上. 大概就是用二次函数去拟合面积. [代码] #include <cstdi ...
- [luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)
传送门 可以二分边长 然后另开两个数组,把x从小到大排序,把y从小到大排序 枚举x,可以得到正方形的长 枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞 其实发现算法的本质之后,x可 ...
- NIO Channel的学习笔记总结
摘自:http://blog.csdn.net/tsyj810883979/article/details/6876603 1.1 非阻塞模式 Java NIO非堵塞应用通常适用用在I/O读写等方 ...