编程介的小学生 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
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页