题目链接:

思路:

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

四维状态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. TUN/TAP/VETH

    TUN/TAP虚拟网络设备为用户空间程序提供了网络数据包的发送和接收能力.他既可以当做点对点设备(TUN),也可以当做以太网设备(TAP). TUN/TAP虚拟网络设备的原理: 在Linux内核中添加 ...

  2. styled-components

    参考: http://www.alloyteam.com/2017/05/guide-styled-components/ https://medium.com/styled-components/g ...

  3. 一、Numpy库与多维数组

    # Author:Zhang Yuan import numpy as np '''重点摘录: 轴的索引axis=i可以理解成是根据[]层数来判断的,0表示[],1表示[[]]... Numpy广播的 ...

  4. boot_mem分配器

    #define alloc_bootmem_low_pages(x) \ __alloc_bootmem_low(x, PAGE_SIZE, ) void * __init __alloc_bootm ...

  5. nrf52832开发配置文件小记

    nrf52832在配置定时器和port事件的时候,需要在nrf_drv_config.h(sdk12.x.0版本)文件中,将相应的使能,比如:#define TIMER0_ENABLED 1否则,是不 ...

  6. JS(DOM 和 BOM)

    JS(DOM 和 BOM) 常说的JS(浏览器执行的JS)包含两部分:1.JS基础知识(语法)(ECMA262标准)2.JS-Web-API(W3C标准) W3C 标准中关于 JS 的规定有:(只管定 ...

  7. Knockout v3.4.0 中文版教程-6-计算监控-可写的计算监控

    2.可写的计算监控 初学者可能想要跳过本节 - 可写的计算监控是相当高级的部分,在大多数情况下不是必需的. 通常,计算监控是一个通过其他监控值计算出的值,因此是只读的. 令人惊讶的是,可以使计算监控值 ...

  8. php5.3.3版本前后变化中php-v和sbin/php-fpm -v

    重装php-fpm试试,遂去http://php-fpm.org/download/想下载个新版本的php-fpm, 结果发现版本大于5.3.3的PHP内部已经集成了php-fpm,不用再另行安装了. ...

  9. 使用HttpClient实现对第三方服务器的请求并接受返回数据

    /* * 创建日期 2017-4-7 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ package com.enfo.int ...

  10. 查看Linux每个进程的流量和带宽

    原文:https://blog.csdn.net/monkeynote/article/details/45867803 作为一个系统管理员,有时候需要搞清楚一台机器上的哪个进程占用了较高的网络带宽. ...