poj3133#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int hash_size=60007;const int INF=100000;int n, m;int map[20][20];int Pow[40];struct node {int hash_chart[hash_size], sz;int msk[hash_size];int dp[hash_size];int next[hash_size];void clear() {sz=0;memset(hash_chart, -1, sizeof(hash_chart));}inline void push(int _msk, int val) {int x=_msk%hash_size;for(int it=hash_chart[x];it!=-1;it=next[it]) {if(msk[it]==_msk) {if(dp[it]>val)dp[it]=val;return ;}}msk[sz]=_msk;dp[sz]=val;next[sz]=hash_chart[x];hash_chart[x]=sz++;}inline int res() {int x=0;for(int it=hash_chart[x];it!=-1;it=next[it]) {if(!msk[it])return dp[it]-2;}return 0;}}hh[2];void solve() {for(int i=0;i<n;i++) {for(int j=0;j<m;j++) {scanf("%d", &map[i][j]);}}int p, t1, t2, i, j, k, t=Pow[m+1], cur, tk, temp, temps, dp;node *src=hh, *des=hh+1;src->clear ();src->push (0, 0);//和2关联的插头为1, 和3关联的插头为2for(i=0;i<n;i++) {for(j=0;j<=m;j++) {des->clear ();for(p=0;p<src->sz ;p++) {k=src->msk [p];dp=src->dp [p];if(j==m) {//每一行的最后一个格的状态转移到下一横行if(k/Pow[m])//最后有插头不能转移continue;des->push (k*3, dp);continue;}t1=(k/Pow[j])%3, t2=(k/Pow[j+1])%3;if(map[i][j]==0) {//当前为0if((t1==0)&&(t2==0)) {//没有插头tk=k+Pow[j]+Pow[j+1];//转到有两个1插头des->push (tk, dp+1);tk=k+(Pow[j]<<1)+(Pow[j+1]<<1);//转到有两个2插头des->push (tk, dp+1);tk=k;des->push (tk, dp);//不变}else if(((!t1)&&t2)||(t1&&(!t2))){//当有一个为有插头temp=k-Pow[j]*t1-Pow[j+1]*t2;//将插头清除temps=(!t1)?t2:t1;tk=temp+Pow[j]*temps;//前一个有插头时des->push (tk, dp+1);tk=temp+Pow[j+1]*temps;//后一个有插头时des->push (tk, dp+1);}else if((t1==t2)&&t1) {//当两个都有插头时tk=k-Pow[j]*t1-Pow[j+1]*t2;//只能转到没有插头des->push (tk, dp+1);}}else if(map[i][j]==1) {//当不能画线时if(!(t1||t2)) {//只有没有插头时可行tk=k;des->push (tk, dp);}}else if(map[i][j]==2) {//当为2时if(!(t1||t2)) {//没插头时只能添加一个1插头tk=k+Pow[j];des->push (tk, dp+1);tk=k+Pow[j+1];des->push (tk, dp+1);}else if(((t1==1)&&(!t2))||((t2==1)&&(!t1))) {//只有一个插头时,必须终止,即没有插头tk=k-Pow[j]*t1-Pow[j+1]*t2;des->push (tk, dp+1);}}else if(map[i][j]==3){//当为3时,情形和2基本一致if(!(t1||t2)) {tk=k+(Pow[j]<<1);des->push (tk, dp+1);tk=k+(Pow[j+1]<<1);des->push (tk, dp+1);}else if(((t1==2)&&(!t2))||((t2==2)&&(!t1))) {tk=k-Pow[j]*t1-Pow[j+1]*t2;des->push (tk, dp+1);}}}swap(src, des);}}printf("%d\n", src->res ());}int main () {Pow[0]=1;for(int i=1;i<32;i++)Pow[i]=Pow[i-1]*3;while(scanf("%d %d", &n, &m), n||m)solve();return 0;}hdu 4064#include <cstdio>#include <iostream>using namespace std;const int hash_size=300007;const __int64 mod=1000000007;int n, m;int Pow[40];int Case;int th[300];struct node { __int64 sum; int size; int msk[hash_size]; __int64 dp[hash_size]; int next[hash_size]; int head[hash_size]; void clear() { sum=0; size=0; memset(head, -1, sizeof(head)); } void push(int _msk, __int64 val) { int x=_msk%hash_size; for(int it=head[x];it!=-1;it=next[it]) { if(msk[it]==_msk) { dp[it]=(dp[it]+val)%mod; return ; } } msk[size]=_msk; dp[size]=val; next[size]=head[x]; head[x]=size++; }}hh[2];void solve() { Case++; scanf("%d %d", &n, &m); node *src=hh, *des=hh+1; src->clear (); int i, j, k, tk, temp, p, q, t1, t2; __int64 val; src->push (0, 1); char str[5]; for(i=0;i<n;i++) { for(j=0;j<=m;j++) { if(j!=m) scanf("%s", str); des->clear (); for(p=0;p<src->size ;p++) { k=src->msk [p]; val=src->dp [p]; if(j==m) { if(k/Pow[m+1]) continue; temp=k%Pow[m]; des->sum =(des->sum +val)%mod; des->push ((temp<<2), val); continue; } t1=(k/Pow[j])%4, t2=(k/Pow[j+1])%4; for(q=0;q<4;q++) { if( ( ( (t1==0) || (t1==th[str[q]]) ) && ( (t2==0) || (t2==th[str[(q+1)&3]]) ) ) ) { temp=k-Pow[j]*t1-Pow[j+1]*t2; tk=temp+Pow[j]*th[str[(q+3)&3]]+Pow[j+1]*th[str[(q+2)&3]]; des->push (tk, val); } } } swap(src, des); } } printf("Case %d: %I64d\n", Case, src->sum );}int main () { int T; th['R']=1, th['F']=2, th['C']=3; Pow[0]=1; for(int i=1;i<32;i++) Pow[i]=(Pow[i-1]<<2); scanf("%d", &T); while(T--) solve(); return 0;}