http://poj.org/problem?id=2965
规模比较小,每一个步骤都用位运算就好了
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #include <iostream> using namespace std; char mp[5][5]; int tm[5][5]; struct node { int x,num,last; node(){} node(int a,int b,int c){x=a;num=b;last=c;} }; node ans[1000000] ; int prin[100000][2]; int ok_p; int ok; int wei[16]={ 63624,62532,61986,61713 ,36744,20292,12066,7953 ,35064,17652,8946,4593 ,34959,17487,8751,4383 }; int vis[1<<16+5]; void get_ans(int xx) { if (xx==1) return ; prin[++ok_p][0]=ans[xx].num/4; prin[ok_p][1]=ans[xx].num%4; get_ans(ans[xx].last); } node que[500100]; int que_ed,que_st ; int main() { que_st= que_ed=0; ok=0;ok_p=0; scanf("%s",mp[0]); scanf("%s",mp[1]); scanf("%s",mp[2]); scanf("%s",mp[3]); int tmp=0; int i,j; for (i=3;i>=0;i--) { for (j=3;j>=0;j--) { tmp<<=1; if (mp[i][j]=='+') // + 0 - 0 tmp++; } } if (!tmp) { printf("0\n"); return 0; } que[++que_ed]=node(tmp,0,1); //sb.push(node(tmp,0,1)); while(que_ed>que_st) { node rt=que[++que_st]; // sb.pop(); ans[++ok]=rt; int tmp=rt.x; int i=0,j=0; int k,h,p; int flag=0; for ( k=0;k<16;k++) { int tt=rt.x^wei[15-k]; if (vis[tt]) continue; vis[tt]=1; node ss(tt,k,ok); // sb.push(ss); que[++que_ed]=ss; if (ss.x==0) { prin[++ok_p][0]=k/4; prin[ok_p][1]=k%4; get_ans(ok); flag=1; break; } } if (flag)break; } printf("%d\n",ok_p); for (i=ok_p;i>=1;i--) { printf("%d %d\n",prin[i][0]+1,prin[i][1]+1); } return 0; }