题目链接:

思路:

这道题是维基百科上面的记忆化搜索的例题。。。

四维状态dp[maxn][5][2][5]分别表示第几根棒子,这根棒子的高度,是否达到题目的要求和使用不同棒子数。那么接下来就是状态转移了。。。要用到位运算推断曾经是否这样的高度的棒子用到没。。。那么这个问题就攻克了。。。

题目:
Number of Locks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1126   Accepted: 551

Description

In certain factory a kind of spring locks is manufactured. There are n slots (1 < n < 17, n is a natural number.) for each lock. The height of each slot may be any one of the 4 values in{1,2,3,4}( neglect unit ). Among the slots of a lock there are at least
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

There is one given data n (number of slots) on every line. At the end of all the input data is -1, which means the end of input.

Output

According to the input data, count the number of locks. Each output occupies one line. Its fore part is a repetition of the input data and then followed by a colon and a space. The last part of it is the number of the locks counted.

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;
}

poj1351Number of Locks(记忆化搜索)的更多相关文章

  1. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  2. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  3. hdu3555 Bomb (记忆化搜索 数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. zoj 3644(dp + 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...

  5. loj 1044(dp+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...

  6. DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects

    题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...

  7. HDU1978 记忆化搜索

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. bzoj4562: [Haoi2016]食物链--记忆化搜索

    这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...

  9. 数位dp/记忆化搜索

    一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an  ...

随机推荐

  1. CSS3-弹性盒模型-FlexBox

    Flex容器属性 display 定义一个Flex容器,根据其取的值来决定是内联还是块.Flex容器会为其内容建立新的伸缩格式化上下文. .container { display: flex; /* ...

  2. 【MySQL】浅谈 varchar(N)

    一  前言  varchar(N) N代表什么意思,能存放多少个中文字符?属于老生常谈的问题了,今天又被一个开发同事问我关于这个问题,索性写一篇文章来具体介绍一下.二 理论知识 先说明一下 MySQL ...

  3. MySQL外键设置 级联删除

    . cascade方式在父表上update/delete记录时,同步update/delete掉子表的匹配记录 . set null方式在父表上update/delete记录时,将子表上匹配记录的列设 ...

  4. kali2018 安装****

    1.安装需要的依赖包: apt-get install qt5-qmake qtbase5-dev libqrencode-dev libappindicator-dev libzbar-dev ro ...

  5. 九度oj 题目1398:移动次数

    题目描述: 众所周知JOBDU旗下的JOBBALA公司是一家以个性.亲民著称的IT公司.在JOBBALA公司成立50周年的日子里,公司CEO组织全体员工登山旅游.按照往常的习惯,导游通常要求游客按照身 ...

  6. OpenJ_Bailian——4115鸣人和佐助(带状态的A*)

    鸣人和佐助 Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status Desc ...

  7. 【二叉树】hdu 1710 Binary Tree Traversals

    acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...

  8. 转载:LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    原文地址:http://yacare.iteye.com/blog/2010049 很多伙伴在更新VS2010,或者卸载VS2012安装2010后,建立Win32 Console Project/MF ...

  9. java面试题之什么是死锁、活锁、饿死和竞态条件?

    死锁:是指两个或两个以上的进程(或线程)在执行过程中,因争夺资源而造成的一种相互等待的现象,若无外力作用,他们将无法推进下去: 活锁:是指两个线程优先级相同,都礼让不走,就这样一直僵持下去: 饿死:在 ...

  10. 使用腾讯互动直播 遇到的坑 'GLIBC_2.14' not found 问题解决

    第一.查看系统glibc版本库 strings /lib64/libc.so.6 |grep GLIBC_ 这里我们可以看到系统中最新的版本是2.12,这里我们升级2.14. 第二.下载和安装glib ...