"The main topic"
Given a m*n matrix, there are some grids for obstructions, and the number of Hamiltonian loops through all non-barrier lattices is obtained.
Ideas
The most typical plug DP. It is divided into three situations:
(1) The current grid has neither the plug nor the left plug.
If there is no barrier between the bottom and the right, create the new component together.
(2) If only left plug or right plug.
Stretching or turning, of course, you have to judge whether there are obstacles.
(3) Neither the plug nor the left plug.
1. If two plugs are not connected (the number is not the same), then the connecting components of the two plugs are combined to mark the same connected block designator, and O (n) scan guarantees the minimum representation;
2. If it is already connected, it is equivalent to having a loop, which can only occur in the last non-barrier lattice.
Because the state is very many, the hash table is used to store the state.
Decode and encode notice, here not to repeat.
"Error Point"
Note that CH is going to be big enough to see the code.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong Longll;7 Const intmaxn= One;8ll dp[2][1<<MAXN];9 Ten voidSolveintMintN) One { A if(m<N) swap (m,n); - intCur=0; -Memset (DP,0,sizeof(DP)); thedp[cur][(1<<n)-1]=1; - for(intI=1; i<=m;i++) - for(intj=1; j<=n;j++) - { +cur^=1; - /*cur to be placed behind the second cycle, written in the triple loop.*/ +memset (Dp[cur],0,sizeof(Dp[cur])); A /*don't forget to clear the current state*/ at for(intk=0; K<= (1<<n)-1; k++) - { - //put up - if(i!=1&&! (k& (1<< (n1)))) - { - intNow= (((k<<1)|1) & ((1<<n)-1)); indp[cur][now]+=dp[1-cur] [K]; - } to //not put + if(k& (1<< (n1))) - { the intNow= ((k<<1) & ((1<<n)-1)); *dp[cur][now]+=dp[1-cur] [K]; $ }Panax Notoginseng //Zofang - if(j!=1&& (k& (1<< (n1)) &&! (k&1)) the { + intNow= (((k<<1)|3) & ((1<<n)-1)); Adp[cur][now]+=dp[1-cur] [K]; the } + } - } $cout<<dp[cur][(1<<n)-1]<<Endl; $ } - - intMain () the { - intM,n;Wuyi while(SCANF ("%d%d",&m,&N)) the { - if(M==n && n==0) Break; Wu solve (m,n); - } About return 0; $}
"Plug DP" Bzoj1814-formula