标签:记忆化搜索
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1140 | Accepted: 559 |
Description
Input
Output
Sample Input
2 3 -1
Sample Output
2: 0 3: 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;
else
return 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));
else
ans+=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));
else
ans+=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;
}
POJ 1351-Number of Locks(记忆化搜索)
标签:记忆化搜索
原文地址:http://blog.csdn.net/qq_16255321/article/details/40985561