2014 Mudanjiang network qualifier question F (Implicit Graph BFS Brute Force Search) zoj3814

Source: Internet
Author: User
Sawtooth puzzle Time Limit: 10 seconds memory limit: 65536 KB

Recently, you found an interesting game calledSawtooth puzzle. This is a single-player game played on a grid with 3x3 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 consideredMeshedIf 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 integerTIndicating 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 3x3 cells. each cell is a monochrome image of 8x8 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

Question: RT
Idea: BFS brute-force search
Pay attention to the following points:
1. Each time the scaling status is expanded, right-handed to 9 blocks
2. There may be many endpoints, because some grids still have the same pattern after rotation.
3. pay attention to the details during processing (this has pitted me for a long time. For example, when I rotate 3rd blocks, I also rotate the Left Block, in fact, there is no block on the left of it)

2014 Mudanjiang network qualifier question F (Implicit Graph BFS Brute Force Search) zoj3814

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.