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.
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.
六边形插头DP诶,好像很兹磁的样子……
码起来其实也不会太麻烦。轮廓线放在列上而不要放在行上会好写很多(其实就是把地图旋转90°),只是可能稍慢。二进制记录有无插头,然后对于一个点的三个入插头就分0,1,2个的情况算,哪一个或者哪两个都是没关系的,3个显然不合法。
然后……只记得我错把n打成m,然后调了很久(我说怎么8 0的时候答案正确,9 0就挂了……)
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define MMH(a,b,c) up(no.x,gx(2*x,a,b,c,no.z),an)
using namespace std;
const int MAXN=;
struct na{
int x,z;
na(int xx,int zz):x(xx),z(zz){}
};
int n,m=,M,x,y,z,a[],k,en,ln;
bool Q[][];
ll f[][MAXN+],mmh;
int v[][MAXN+];
queue <na> q;
inline int gx(int x,int q1,int q2,int q3,int k){q1^=;q2^=;q3^=;k|=(<<x)|(<<(x+))|(<<(x+));k^=(q1<<x)|(q2<<(x+))|(q3<<(x+));return k;}
inline void up(int x,int z,ll lj){
k=(++x)&;
if (v[k][z]!=x) v[k][z]=x,f[k][z]=,q.push(na(x,z));
f[k][z]=f[k][z]+lj;
}
char c[];
int main(){
register int i,j;
while(~scanf("%d%d",&n,&M)){
mmh=;
memset(Q,,sizeof(Q));
memset(v,,sizeof(v));
for (i=;i<n;i++)
for (j=;j<m;j++) Q[i][j]=;
for (i=;i<M;i++)
scanf("%s",&c),Q[n-(c[]-'A')-][c[]-'A']=;
en=n*m;
while(!Q[en%n][en/n]&&en>) en--;en++;
f[][]=v[][]=;
q.push(na(,));
while(!q.empty()){
na no=q.front();q.pop();
ll an=f[no.x&][no.z];
if (ln!=no.x) ln=no.x,y=ln/n,x=ln-y*n;
if (x==&&(y&)==) no.z<<=;
for (i=,j=no.z;j;i++,j>>=) a[i]=j%;
for (;i<=(n<<);i++) a[i]=;
if (!Q[x][y]){
if (no.x==en) mmh+=an;else
MMH(,,);
}else
if (y&){
if (a[*x]+a[*x+]+a[*x+]==){
if (Q[x][y+]&Q[x+][y+]) MMH(,,);
if (Q[x][y+]&Q[x+][y]) MMH(,,);
if (Q[x+][y+]&Q[x+][y]) MMH(,,);
}else if (a[*x]+a[*x+]+a[*x+]==){
if (Q[x][y+]) MMH(,,);
if (Q[x+][y+]) MMH(,,);
if (Q[x+][y]) MMH(,,);
}else if (!(a[*x]&a[*x+]&a[*x+])) MMH(,,);
}else{
if (a[*x]+a[*x+]+a[*x+]==){
if (x) if (Q[x-][y+]&Q[x][y+]) MMH(,,);
if (x) if (Q[x-][y+]&Q[x+][y]) MMH(,,);
if (Q[x][y+]&Q[x+][y]) MMH(,,);
}else if (a[*x]+a[*x+]+a[*x+]==){
if (x) if (Q[x-][y+]) MMH(,,);
if (Q[x][y+]) MMH(,,);
if (Q[x+][y]) MMH(,,);
}else if (!(a[*x]&a[*x+]&a[*x+])) MMH(,,);
}
}
printf("%llu\n",mmh);
}
}
1490MS 52920KB