zoj 3814 Sawtooth Puzzle(搜索-bfs)

来源:互联网 发布:windows桌面主题 编辑:程序博客网 时间:2024/05/08 07:18
Sawtooth Puzzle

Time Limit: 10 Seconds      Memory Limit: 65536 KB

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 10 0 1 11 0 0 10 0 1 11 1 1 01 0 1 11 1 1 10 0 1 00 1 1 0

Sample Output

6

Hint

Here is the detailed solution to the sample input: (1,2) (1,2) (1,2) (1,2) (1,3) (3,1).

This problem comes from an interesting game. You can play it here (Level 2-2).


每个小格子有4中状态,总共9个格子,有4^9个状态。

数据结构设计:

struct Cell{
    string patten[maxn];
    int left , top , right , bottom;
};//记录每个小格子状态

Cell ini_Puzzle[9][4]记录9个格子每个格子的4中状态

struct Node{
    int sta[maxn];
    Node(int c0 = 0 ,int c1 = 0 , int c2 = 0 , int c3 = 0 , int c4 = 0 , int c5 = 0 , int c6 = 0 , int c7 = 0 , int c8 = 0){
        sta[0] = c0;
        sta[1] = c1;
        sta[2] = c2;
        sta[3] = c3;
        sta[4] = c4;
        sta[5] = c5;
        sta[6] = c6;
        sta[7] = c7;
        sta[8] = c8;
        sta[9] = 0;
    }
};//用于记录当前9个格子分别处于哪个状态


#include <iostream>#include <cstdio>#include <queue>#include <vector>#include <cstring>#include <string>#include <algorithm>using namespace std;const int maxn = 10;struct Cell{    string patten[maxn];    int left , top , right , bottom;};struct Node{    int sta[maxn];    Node(int c0 = 0 ,int c1 = 0 , int c2 = 0 , int c3 = 0 , int c4 = 0 , int c5 = 0 , int c6 = 0 , int c7 = 0 , int c8 = 0){        sta[0] = c0;        sta[1] = c1;        sta[2] = c2;        sta[3] = c3;        sta[4] = c4;        sta[5] = c5;        sta[6] = c6;        sta[7] = c7;        sta[8] = c8;        sta[9] = 0;    }};Cell ini_Puzzle[maxn][4] , aim_Puzzle[maxn];vector<int> aim_Sta[maxn];int step[4][4][4][4][4][4][4][4][4];int vis[maxn];void initial(){    memset(step , 0 , sizeof step);    for(int i = 0; i < maxn; i++){        for(int j = 0; j < 4; j++){            for(int k = 0; k < maxn; k++){                ini_Puzzle[i][j].patten[k].clear();            }        }        aim_Sta[i].clear();    }}void readcase(){    for(int k = 0; k < 3; k++){        for(int i = 0; i < 8; i++){            cin >> ini_Puzzle[k*3][0].patten[i] >> ini_Puzzle[k*3+1][0].patten[i] >> ini_Puzzle[k*3+2][0].patten[i];        }    }    for(int k = 0; k < 3; k++){        for(int i = 0; i < 8; i++){            cin >> aim_Puzzle[k*3].patten[i] >> aim_Puzzle[k*3+1].patten[i] >> aim_Puzzle[k*3+2].patten[i];        }    }    for(int i = 0; i < 9; i++){        scanf("%d%d%d%d" , &ini_Puzzle[i][0].left , &ini_Puzzle[i][0].top , &ini_Puzzle[i][0].right , &ini_Puzzle[i][0].bottom);    }}void Rotate(int p){    for(int i = 1; i < 4; i++){        for(int j = 0; j < 8; j++){            for(int k = 7; k >= 0; k--){                ini_Puzzle[p][i].patten[j].push_back(ini_Puzzle[p][i-1].patten[k][j]);            }        }        ini_Puzzle[p][i].left = ini_Puzzle[p][i-1].bottom;        ini_Puzzle[p][i].top = ini_Puzzle[p][i-1].left;        ini_Puzzle[p][i].right = ini_Puzzle[p][i-1].top;        ini_Puzzle[p][i].bottom = ini_Puzzle[p][i-1].right;    }}void get_AimSta(int p){    for(int i = 0; i < 4; i++){        bool flag = true;        for(int r = 0; r < 8; r++){            if(ini_Puzzle[p][i].patten[r] != aim_Puzzle[p].patten[r]){                flag = false;                break;            }        }        if(flag){            aim_Sta[p].push_back(i);        }    }}bool check(int sta[maxn]){    for(int i = 0; i < 9; i++){        bool flag = false;        for(int j = 0; j < aim_Sta[i].size(); j++){            if(sta[i] == aim_Sta[i][j]){                flag = true;                break;            }        }        if(flag == false) return false;    }    return true;}bool try_Rotate(int p , int sta[maxn] , int dir){    if(vis[p] != -1){        if((sta[p]+dir+4)%4 == vis[p])return true;        return false;    }    int L = ini_Puzzle[p][sta[p]].left;    int T = ini_Puzzle[p][sta[p]].top;    int R = ini_Puzzle[p][sta[p]].right;    int B = ini_Puzzle[p][sta[p]].bottom;    vis[p] = (sta[p]+dir+4)%4;    int left = p - 1, top = p - 3, right = p + 1, bottom = p + 3;    if(left == 2 || left == 5 || left < 0) left = -1;    if(top < 0) top = -1;    if(right >= 9 || right == 3 || right == 6) right = -1;    if(bottom > 8) bottom = -1;    if(left != -1 && L == 1 && ini_Puzzle[left][sta[left]].right == 1 && !try_Rotate(left , sta , -1*dir)) return false;    if(right != -1 && R == 1 && ini_Puzzle[right][sta[right]].left == 1 && !try_Rotate(right , sta , -1*dir)) return false;    if(top != -1 && T == 1 && ini_Puzzle[top][sta[top]].bottom == 1 && !try_Rotate(top , sta , -1*dir)) return false;    if(bottom != -1 && B == 1 && ini_Puzzle[bottom][sta[bottom]].top == 1 && !try_Rotate(bottom , sta , -1*dir)) return false;    return true;}void computing(){    for(int i = 0; i < 9; i++) Rotate(i);    for(int i = 0; i < 9; i++) get_AimSta(i);    for(int i = 0; i < 9; i++){        if(aim_Sta[i].size() == 0){            printf("-1\n");            return;        }    }    queue<Node> q;    Node node = Node(0 , 0 , 0 , 0 , 0 , 0 , 0 , 0);    step[0][0][0][0][0][0][0][0][0] = 1;    q.push(node);    while(!q.empty()){        node = q.front();        q.pop();        int tstep = step[node.sta[0]][node.sta[1]][node.sta[2]][node.sta[3]][node.sta[4]][node.sta[5]][node.sta[6]][node.sta[7]][node.sta[8]];        if(check(node.sta)){            printf("%d\n" , tstep-1);            return;        }        for(int i = 0; i < 9; i++){            for(int j = 0; j < maxn; j++) vis[j] = -1;            if(try_Rotate(i , node.sta , 1)){                for(int j = 0; j < 9; j++) if(vis[j] == -1) vis[j] = node.sta[j];                if(step[vis[0]][vis[1]][vis[2]][vis[3]][vis[4]][vis[5]][vis[6]][vis[7]][vis[8]] == 0){                    step[vis[0]][vis[1]][vis[2]][vis[3]][vis[4]][vis[5]][vis[6]][vis[7]][vis[8]] = tstep+1;                    q.push(Node(vis[0],vis[1],vis[2],vis[3],vis[4],vis[5],vis[6],vis[7],vis[8]));                }            }        }    }    printf("-1\n");}int main(){    int T;    scanf("%d" , &T);    while(T--){        initial();        readcase();        computing();    }    return 0;}


0 0