1519. Formula 1
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Background
Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it.
Problem
Who will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But our heroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said.
It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangle
N*
M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for
N =
M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here.
Input
The first line contains the integer numbers
N and
M (2 ≤
N,
M ≤ 12). Each of the next
N lines contains
M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located. There are at least 4 cells without gopher holes.
Output
You should output the desired number of ways. It is guaranteed, that it does not exceed 2
63-1.
Samples
input | output |
---|---|
4 4 **.. .... .... .... | 2 |
4 4 .... .... .... .... | 6 |
题意:求N*M的格子中 有多少种哈密顿回路
分析:CDQ插头DP 论文第一道题,看了半年 ,搁置很久才开始写,今天终于写出来了。
做这道题前置技能:状态压缩+轮廓线+HASH。
在CDQ论文(基于连通性状态压缩的动态规划问题 )中已经把所有情况讲的很清楚了,
网上有两种写法,一种最小表示法,我用的是论文中的方法,括号法,3进制用4进制表示,贴出代码共参观,还有 TEST12 的数据。
代码:
#include<bits/stdc++.h>
#define mem(p,k) memset(p,k,sizeof(p));
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=14;
const int limitnum=1e6;
int mp[N][N],n,m;
int cur,tot[2],ei,ej;
int has[limitnum],bit[20];
ll state[2][limitnum],sum[2][limitnum];
char s[N];
void init(){
mem(tot,0);
mem(has,0);
mem(state,0);
mem(sum,0);
mem(mp,0);
ei=ej=-1;
for(int i=0;i<n;i++){
scanf("%s",s);
for(int j=0;j<m;j++){
mp[i][j]= (s[j]=='.');
if(mp[i][j])ei=i,ej=j;
}
}
for(int i=0;i<20;i++){
bit[i]=i<<1;
}
}
void update(ll s,ll val){
int k=s%limitnum;
while(has[k]){
if(state[cur][has[k]]==s){
sum[cur][has[k]]+=val;return;
}
k++;
if(k==limitnum)k=0;
}
has[k]=++tot[cur];
state[cur][tot[cur]]=s;
sum[cur][tot[cur]]=val;
}
void dfs(int k){
if(!k)return;
dfs(k>>1);
cout<<(k&1);
}
void solve(){
cur=0;
tot[cur]=1;
state[cur][1]=0;
sum[cur][1]=1;
ll ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cur^=1;
mem(state[cur],0);
mem(sum[cur],0);
mem(has,0);
tot[cur]=0;
//cout<<i<<" "<<j<<" ::"<<endl;
for(int k=1;k<=tot[cur^1];k++){
int p=state[cur^1][k]&(3<<bit[j]);
int q=state[cur^1][k]&(3<<bit[j+1]);
ll f=state[cur^1][k],val=sum[cur^1][k];
if(mp[i][j]){
if(p==0 && q==0){
if(mp[i][j+1] && mp[i+1][j])update(f|(1<<bit[j])|(2<<bit[j+1]),val);
}
else if(!p && q || p && !q){
if(p && mp[i+1][j] || q && mp[i][j+1])update(f,val);
f^=p|q;
p<<=2;
q>>=2;
if(p && mp[i][j+1] || q && mp[i+1][j])update(f|p|q,val);
}
else{
f^=q|p;
p>>=bit[j];
q>>=bit[j+1];
int sum=1,u;
if(p==1 && q==1){
for(u=j+2; u<m ;u++){
int cnt=f>>bit[u]&3;
if(cnt==1)sum++;
if(cnt==2)sum--;
if(sum==0){
f-=1<<bit[u];
break;
}
}
update(f,val);
}
else if(p==2 && q==2){
for(u=j-1; u>=0 ;u--){
int cnt=f>>bit[u]&3;
if(cnt==1)sum--;
if(cnt==2)sum++;
if(sum==0){
f+=1<<bit[u];
break;
}
}
update(f,val);
}
else if(p==2 && q==1){
update(f,val);
}
else{
if(ei==i && ej==j){
ans+=val;
}
}
}
}
else{
if(p+q==0){
update(f,val);
}
}
}
if(ei==i && ej==j)break;
}
if(ei==i)break;
for(int i=1;i<=tot[cur];i++)state[cur][i]<<=2;
}
cout<<ans<<endl;
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
if(ei<0){
cout<<0<<endl;continue;
}
solve();
}
return 0;
}
/*
12 12
............
............
............
............
............
............
............
............
............
............
............
............
10 10
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
8 8
........
........
........
........
........
........
........
........
*/