Recently, you found an interesting game called Sawtooth Puzzle. This is a single-player game played on a grid with 3 x 3 cells. Each cell contains a part of an image. Besides, each edge of a cell will be either smooth or saw-toothed. Two edge-adjacent cells are considered as meshed if their adjacent edges are both saw-toothed.
The game contains zero or more steps. At each step, the player can choose one cell, rotate it by 90 degrees clockwise. At the beginning of rotation, any cell which is going to rotate will drive other cells to rotate together if they are meshed. Of course, the driven (passive) cell will rotate in the opposite direction against the driver (active) cell. The following image demonstrates the procedure of rotating the middle cell clockwise:
Given the initial image and target image of the puzzle, please find out the minimum number of steps needed to solve it.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The input data consists of three parts. The first part represents the initial image of the puzzle. It contains 26 lines. Each line has 26 characters (excluding the line break '\n'). The image is composed of 3 x 3 cells. Each cell is a monochrome image of 8 x 8 pixels ('#' for black, '.' for white). These cells are separated by space characters of 1 pixel width.
The second part represents the target image of the puzzle. It has the same format as which described above.
The last part contains 9 lines. Each line has 4 integers indicating the edge type (0 for smooth, 1 for saw-toothed) of a cell. The cells are listed in the order of top-left, top-middle, top-right, middle-left, ... , till bottom-right. For each cell, the edge type are given in the order of left, top, right and bottom.
There is an empty line between any two adjacent parts (including of different cases).
Output
For each test case, output the minimum number of steps needed to solve the puzzle. If there is no solution, output -1 instead.
Sample Input
1 ......#. ........ ........ ......## ........ ........ .......# #....... ........ .......# ##...... ...##... .......# ###..... .###.... .......# ####...# ####.... .......# ######## ###..... .......# ######## ###..... ........ ######## ###..... .......# ######## ##...... .....### ######## ##...... ....#### ######## ###..... ..###### ######## ####.... ######## ######## #####... ...##### ######## ######.. ........ ######## #######. ........ ........ ..###### ........ .....### ........ ........ ######## ........ ........ .####### ........ ........ ...##### ........ ........ .....### ........ ........ .......# ........ ........ ........ ........ ......#. ........ ........ ......## ........ ........ .......# #....... ........ .......# ##...... ...##... .......# ###..... .###.... .......# ####...# ####.... .......# ######## ###..... .......# ######## ###..... ........ ######## ###..... .......# ######## ##...... .....### ######## ##...... ....#### ######## ###..... ..###### ######## ####.... ######## ######## #####... ...##### ######## ######.. ........ ######## #######. ........ .######. ..###### ........ .#####.. ........ ........ .#####.. ........ ........ ..###... ........ ........ ..###... ........ ........ ..##.... ........ ........ ..##.... ........ ........ ..#..... ........ 0 1 0 1 0 0 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 0 0 1 0 0 1 1 0
Sample Output
6
#include
#include
#include
#include
#include
using namespace std; char mp[9][10][10]; char gmp[9][10][10]; int ha[9][4]; int hehe[9][4]; //标记终点的未压缩的状态 (8维) int en[300000]; //标记终点的压缩后的状态 (一个数字) int vis[300000]; //记录步数,顺便标记状态 int q[300000]; //队列,用来扩展整个状态 int qsmall[15]; //队列,用来扩展每个方块的旋转的状态(周围四个) bool fx(int vis) { for(int i=0;i<8;i++) for(int j=0;j<8;j++) if(mp[vis][i][j]!=gmp[vis][i][j])return false; return true; } void clockwise(int vis) { char temp[8][8]; for(int i=0;i<8;i++) for(int j=0;j<8;j++) { temp[i][j]=mp[vis][7-j][i]; } for(int i=0;i<8;i++) for(int j=0;j<8;j++) mp[vis][i][j]=temp[i][j]; } inline int add(int a,int b) { a+=b; if(a>=4)a-=4; return a; } inline int sub(int a,int b) { a-=b; if(a<0)a+=4; return a; } inline bool gx(int x) { if(x<0||x>8)return false; return true; } int temp[9]; int nowha[9][4]; int rot(int root) //旋转 { int front=0,rear=0; bool vs[9],vt[9]; int tem[9]; for(int i=0;i<9;i++)tem[i]=temp[i]; memset(vs,false,sizeof(vs)); memset(vt,false,sizeof(vt)); qsmall[rear++]=root; vs[root]=vt[root]=true; tem[root]=add(temp[root],1); int pos[4]; while(front
=0;--i) { temp[i]=tf%4; //提取状态 tf/=4; int v=temp[i]; for(int j=0;j<4;++j) //对每个位置的方块处理出它的毛边 nowha[i][j]=ha[i][add(sub(j,v),4)]; } for(int i=0;i<9;++i) //对9个位置的方块进行右旋(走一步),拓展状态 { int ans=rot(i); if(ans!=-1){ //对旋转之后的状态进行判断,如果没有拓展过就进队 vis[ans]=vis[f]+1; //记录步数 if(en[ans])return vis[ans]-1; //走到终点 q[rear++]=ans; } } } return -1; } void dfs(int cent,int v) { if(cent==9) { en[v]=1; return; } for(int i=0;i<4;i++) { if(hehe[cent][i]) { dfs(cent+1,v*4+i); } } } int main() { int t; scanf("%d",&t); while(t--) { for(int i=0;i<24;i++) { int cnt=i/8*3; for(int j=0;j<3;j++) { scanf("%s",mp[cnt+j][i%8]); } } for(int i=0;i<24;i++) { int cnt=i/8*3; for(int j=0;j<3;j++) { scanf("%s",gmp[cnt+j][i%8]); } } for(int i=0;i<9;i++) for(int j=0;j<4;j++)scanf("%d",&ha[i][j]); memset(hehe,0,sizeof(hehe)); memset(en,0,sizeof(en)); for(int i=0;i<9;i++) { for(int k=0;k<4;k++) { bool ok=fx(i); if(ok){ hehe[i][k]=1; } clockwise(i); } } dfs(0,0); //标出所有的终点的状态 int ans=bfs(); //开始走 printf("%d\n",ans); } }