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=2197152; struct na{ int x,z; na(int xx,int zz):x(xx),z(zz){} }; int n,m=8,M,x,y,z,a[22],k,en,ln; bool Q[20][20]; ll f[2][MAXN+1],mmh; int v[2][MAXN+1]; queue <na> q; inline int gx(int x,int q1,int q2,int q3,int k){q1^=1;q2^=1;q3^=1;k|=(1<<x)|(1<<(x+1))|(1<<(x+2));k^=(q1<<x)|(q2<<(x+1))|(q3<<(x+2));return k;} inline void up(int x,int z,ll lj){ k=(++x)&1; if (v[k][z]!=x) v[k][z]=x,f[k][z]=0,q.push(na(x,z)); f[k][z]=f[k][z]+lj; } char c[5]; int main(){ register int i,j; while(~scanf("%d%d",&n,&M)){ mmh=0; memset(Q,0,sizeof(Q)); memset(v,0,sizeof(v)); for (i=0;i<n;i++) for (j=0;j<m;j++) Q[i][j]=1; for (i=0;i<M;i++) scanf("%s",&c),Q[n-(c[0]-'A')-1][c[1]-'A']=0; en=n*m; while(!Q[en%n][en/n]&&en>0) en--;en++; f[0][0]=v[0][0]=1; q.push(na(0,0)); while(!q.empty()){ na no=q.front();q.pop(); ll an=f[no.x&1][no.z]; if (ln!=no.x) ln=no.x,y=ln/n,x=ln-y*n; if (x==0&&(y&1)==0) no.z<<=2; for (i=0,j=no.z;j;i++,j>>=1) a[i]=j%2; for (;i<=(n<<1);i++) a[i]=0; if (!Q[x][y]){ if (no.x==en) mmh+=an;else MMH(0,0,0); }else if (y&1){ if (a[2*x]+a[2*x+1]+a[2*x+2]==0){ if (Q[x][y+1]&Q[x+1][y+1]) MMH(1,1,0); if (Q[x][y+1]&Q[x+1][y]) MMH(1,0,1); if (Q[x+1][y+1]&Q[x+1][y]) MMH(0,1,1); }else if (a[2*x]+a[2*x+1]+a[2*x+2]==1){ if (Q[x][y+1]) MMH(1,0,0); if (Q[x+1][y+1]) MMH(0,1,0); if (Q[x+1][y]) MMH(0,0,1); }else if (!(a[2*x]&a[2*x+1]&a[2*x+2])) MMH(0,0,0); }else{ if (a[2*x]+a[2*x+1]+a[2*x+2]==0){ if (x) if (Q[x-1][y+1]&Q[x][y+1]) MMH(1,1,0); if (x) if (Q[x-1][y+1]&Q[x+1][y]) MMH(1,0,1); if (Q[x][y+1]&Q[x+1][y]) MMH(0,1,1); }else if (a[2*x]+a[2*x+1]+a[2*x+2]==1){ if (x) if (Q[x-1][y+1]) MMH(1,0,0); if (Q[x][y+1]) MMH(0,1,0); if (Q[x+1][y]) MMH(0,0,1); }else if (!(a[2*x]&a[2*x+1]&a[2*x+2])) MMH(0,0,0); } } printf("%llu\n",mmh); } }