There is a hive in the village. Like this. There are 8 columns(from A to H) in this hive. Different colums have the same number of grids. Every grid has its own coordinate, which is formed by two uppercases, representing the row index and the column index. The row index starts from A. And the hive has less than ten rows in total. The following figure shows a hive with two rows.
![Photo](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=The_Hive_II_1.jpg)
There is honey in some grids. A naughty bee discovers this special hive and hopes to eat all honey in the hive. However, this strange bee sets some rules for itself while eating. They are descirbed as following:
- It must eat the honey by choosing a circuit and then eat all honey that is in the chosen circuit.
- Honey will disappear immediately after the bee eats it.
- All grids which are in the circuit should has honey in it.
- The length of the circuit should be no less than 3.
- The bee can choose more than one circuit to eat honey.
Given the hive and the honey in it, how many different ways can this naughty bee eat all honey in the hive?
Input
There are multiple test cases.
For each case, there are two integers N(0 < N ≤ 10) and M(0 ≤ M ≤ N * 8) in the first line. N represents the size of the hive, which means there are N rows in the hive in total. All grids have honey in it except for those M grids listed in the following line. Each empty grid is described by its coordinate(using two uppercases).
Output
For each case, output the number of different ways the bee can eat all honey in the hive. It's guaranteed that the answer does not exceed 263 - 1.
Sample Input
3 5 BB CD BF AH CG
Sample Output
3
Hint
The following figure shows all different ways for the sample. The black grids represent those which are initially empty.
![Photo](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=The_Hive_II_2.jpg)
解题报告:插头DP,转过来即可。每次处理3个插头,注意换行。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int now,pre;
int n,m;
const int maxn=499979;
bool maze[12][12];
struct Node
{
int H[maxn];
int S[maxn];
LL N[maxn];
int size;
void init()
{
size=0;
memset(H,-1,sizeof(H));
}
void push(int SS,LL num)
{
int s=SS%maxn;
while( ~H[s] && S[H[s]]!=SS )
s=(s+1)%maxn;
if( ~H[s] )
{
N[H[s]]+=num;
}
else
{
S[size]=SS;
N[size]=num;
H[s]=size++;
}
}
LL get(int SS)
{
int s=SS%maxn;
while( ~H[s] && S[H[s]]!=SS )
s=(s+1)%maxn;
if( ~H[s] )
return N[H[s]];
else
return 0;
}
} dp[2];
int get(int S,int p)
{
return (S>>p)&1;
}
void set(int &S,int p,int v)
{
S^=get(S,p)<<p;
S^=v<<p;
}
void init()
{
memset(maze,0,sizeof(maze));
for(int i=1;i<=8;i++) for(int j=1;j<=n;j++)
maze[i][j]=true;
while(m--)
{
char str[20];
scanf("%s",str);
maze[str[1]-'A'+1][str[0]-'A'+1]=false;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
now=1;
pre=0;
dp[now].init();
dp[now].push(0,1);
int dl=0,dr=1,sh=2;
for(int i=1;i<=8;i++)
{
for(int j=1;j<=n;j++)
{
swap(now,pre);
dp[now].init();
for(int s=0;s<dp[pre].size;s++)
{
int S=dp[pre].S[s];
LL num=dp[pre].N[s];
int sta=j*2-2;
int p=get(S,sta);
int q=get(S,sta+1);
int r=get(S,sta+2);
if(maze[i][j]==0)
{
if(p+q+r==0)
dp[now].push(S,num);
continue;
}
if(p+q+r==0)
{
if(maze[i+1][j+dl] && maze[i+1][j+dr])
{
set(S,sta,1);
set(S,sta+1,1);
set(S,sta+2,0);
dp[now].push(S,num);
}
if(maze[i+1][j+dl] && maze[i][j+1])
{
set(S,sta,1);
set(S,sta+1,0);
set(S,sta+2,1);
dp[now].push(S,num);
}
if(maze[i+1][j+dr] && maze[i][j+1])
{
set(S,sta,0);
set(S,sta+1,1);
set(S,sta+2,1);
dp[now].push(S,num);
}
}
else if(p+q+r==1)
{
if(maze[i+1][j+dl])
{
set(S,sta,1);
set(S,sta+1,0);
set(S,sta+2,0);
dp[now].push(S,num);
}
if(maze[i+1][j+dr])
{
set(S,sta,0);
set(S,sta+1,1);
set(S,sta+2,0);
dp[now].push(S,num);
}
if(maze[i][j+1])
{
set(S,sta,0);
set(S,sta+1,0);
set(S,sta+2,1);
dp[now].push(S,num);
}
}
else if(p+q+r==2)
{
set(S,sta,0);
set(S,sta+1,0);
set(S,sta+2,0);
dp[now].push(S,num);
}
}
}
for(int s=0;s<dp[now].size;s++)
dp[now].S[s]<<=sh;
dl=-1-dl;
dr=1-dr;
sh=2-sh;
}
printf("%lld\n",dp[now].get(0));
}
}