编程介的小学生 2017-08-28 00:55 采纳率: 20.5%
浏览 640
已采纳

Sawtooth Puzzle

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

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-09-11 15:43
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了