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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小动物死了孩子很伤心怎么办 向客人开错房间怎么办 工伤认定没有证人证言怎么办? 一个月没来月经怎么办14岁 刮了胡子下巴痒怎么办 月经多怎么办喝什么好 大姨妈不调怎么办来了 多人被骗没证据怎么办 一直坏肚子拉水怎么办 犯人在监狱有高危高血压怎么办 丈夫出轨他死都不承认怎么办 没写欠条的债该怎么办 被尾随回家知道住处怎么办 微信记录全没了怎么办 微信记录没有了怎么办 ipad登录不了下不了软件怎么办 苹果手机打开自动时间不对怎么办 魅族手机时间不同步怎么办 小米手环时间不准怎么办 电脑网络dns配置错误怎么办 去泰国手机要打电话怎么办 苹果6s音量小怎么办 三星0n7充电慢怎么办 在国外接受不到验证码怎么办 ip地址错误网络无法连通怎么办 注册微信收不到验证码怎么办 安卓手机收不到短信怎么办 手机被短信验证码轰炸怎么办 美国访学一年手机卡怎么办方便 小狗吃了葡萄皮怎么办 ios迅雷下载不了的资源怎么办 ipad软件商店内容少怎么办? 脸摔伤后留下黑印怎么办 不确定孩子是不是老公的怎么办 孩子接种证丢了怎么办 孩子的出生证丢了怎么办 私秘边上肿了怎么办 书法作品少写一个字怎么办 炉石传说ios闪退怎么办 ck手表表链大了怎么办 天梭手表卡扣坏了怎么办