
ZOJ Problem Set  3814
Recently, you found an interesting game called Sawtooth Puzzle. This is a singleplayer 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 sawtoothed. Two edgeadjacent cells are considered as meshed if their adjacent edges are both sawtoothed. 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. InputThere 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 sawtoothed) of a cell. The cells are listed in the order of topleft, topmiddle, topright, middleleft, ... , till bottomright. 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). OutputFor each test case, output the minimum number of steps needed to solve the puzzle. If there is no solution, output 1 instead. Sample Input1 ......#. ........ ........ ......## ........ ........ .......# #....... ........ .......# ##...... ...##... .......# ###..... .###.... .......# ####...# ####.... .......# ######## ###..... .......# ######## ###..... ........ ######## ###..... .......# ######## ##...... .....### ######## ##...... ....#### ######## ###..... ..###### ######## ####.... ######## ######## #####... ...##### ######## ######.. ........ ######## #######. ........ ........ ..###### ........ .....### ........ ........ ######## ........ ........ .####### ........ ........ ...##### ........ ........ .....### ........ ........ .......# ........ ........ ........ ........ ......#. ........ ........ ......## ........ ........ .......# #....... ........ .......# ##...... ...##... .......# ###..... .###.... .......# ####...# ####.... .......# ######## ###..... .......# ######## ###..... ........ ######## ###..... .......# ######## ##...... .....### ######## ##...... ....#### ######## ###..... ..###### ######## ####.... ######## ######## #####... ...##### ######## ######.. ........ ######## #######. ........ .######. ..###### ........ .#####.. ........ ........ .#####.. ........ ........ ..###... ........ ........ ..###... ........ ........ ..##.... ........ ........ ..##.... ........ ........ ..#..... ........ 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 Output6 HintHere 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 22). Author: JIANG, Kai Source: The 2014 ACMICPC Asia Mudanjiang Regional First Round 