POJ 1351-Number of Locks(记忆化搜索)
来源:互联网 发布:什么是栅格数据 编辑:程序博客网 时间:2024/06/05 07:32
Number of Locks
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1140 Accepted: 559
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
23-1
Sample Output
2: 03: 8
4维的记忆化搜索。。英语太渣。。看的题解。
题意:有一排n个柱子。每个柱子的高度可以为{1,2,3,4} 求问 排列中存在相邻两个柱子高度差为3 ,且使用了至少3种高度,
然后就是裸记忆化暴搜了。。题解用的状态压缩来判断当前状态使用了几种高度以及这种高度以前有没有用过。。不是太懂状态压缩,然后我觉着开标记数组也可以解决这个问题。。然后交上去A掉了发现内存耗的好多。。
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>#define ll long long#define INF 0x3f3f3f3f#define pp pair<int,int>using namespace std;ll dp[18][5][2][4];bool vis[5];int n;ll dfs(int x,int high,bool ok,int use){if(dp[x][high][ok][use]!=-1)return dp[x][high][ok][use];if(x==n){if(use>=3&&ok)return 1;elsereturn 0;}ll ans=0;for(int i=1;i<=4;i++){if(!vis[i]){vis[i]=1;if(ok||(high==1&&i==4)||(high==4&&i==1))ans+=dfs(x+1,i,1,min(use+1,3));elseans+=dfs(x+1,i,0,min(use+1,3));vis[i]=0;}else{if(ok||(high==1&&i==4)||(high==4&&i==1))ans+=dfs(x+1,i,1,min(use,3));elseans+=dfs(x+1,i,0,min(use,3));}}return dp[x][high][ok][use]=ans;}int main(){while(~scanf("%d",&n)&&n!=-1){if(n<3){printf("%d: 0\n",n);continue;}memset(dp,-1,sizeof(dp));dfs(0,0,0,0);printf("%d: %lld\n",n,dp[0][0][0][0]);}return 0;}
1 0
- POJ 1351-Number of Locks(记忆化搜索)
- POJ 1351 Number of Locks (记忆化搜索 状态压缩)
- 【DFS(记忆化)】poj 1351 Number of Locks
- POJ1351 Number of Locks 记忆化搜索
- POJ1351:Number of Locks(记忆化搜索)
- poj1351Number of Locks(记忆化搜索)
- POJ1351 Number of Locks(DP:记忆化搜索)
- poj 1351 Number of locks
- POJ-1351 Number of Locks
- [POJ 1351] Number of Locks
- POJ 1351 Number of Locks 笔记
- poj 1143 Number Game (记忆化搜索)
- pku1351 Number of Locks
- Number of Locks(记搜)
- poj 1143 Number Game 博弈 记忆化状态压缩搜索
- 【poj 1143】Number Game 博弈论+记忆化搜索
- 【记忆化搜索】POJ
- 记忆化搜索——POJ 1351
- Linux下各种查找命令(find, grep, which, whereis, locate)
- 032数值的整数次方(keep it up)
- 信管1132-05:第四章知识结构框图
- 找出矩阵中含有0最多的一行(find the longest row of zero)
- 关于Chartboost的视频广告
- POJ 1351-Number of Locks(记忆化搜索)
- Monty Python_From Wikipedia
- CF - 474 - D. Flowers(dp)
- nfs: server 192.168.1.150 not responding, still trying
- PDF笔记(二):Itext 实际项目案例
- java 数字签名快捷方法
- 常见进程间的通信方式
- Zookeeper的单机安装和调试
- mybatis加Spring项目: 解决There is no getter for property named '***' in 'class java.lang.String'问题