Poj 3221 diamond puzzle

Source: Internet
Author: User

Description

A diamond puzzle is played on a tessellated hexagon like the one shown inFigure 1Below. and in this problem the faces produced by the tessellation are identified as they are numbered in the same figure. if two faces share a side, they are called neighboring faces. thus, even-numbered faces have three neighboring faces, while odd-numbered faces have only two. at any point during the play of the puzzle, six of the seven faces hold a unique digit ranging from 1 to 6, and the other one is empty. A move in the puzzle is to move a digit from one face to a neighboring empty one.

Starting from any configuration, some series of moves can always make the puzzle look identical to either one shown inFigures 2And3. Your task is to calculate the minimum number of moves to make it become the one inFigure 2.

Input

The input contains multiple test cases. The first contains an integerN(0 ≤N≤ 5,040), the number of test cases. Then followNLines, each with a permutation of {0, 1, 2, 3, 4, 5, 6} describing a starting configuration of the Puzzle.ITh digit in the permutation is the one in the face numberedI? 1. A zero means the face is empty.

Output

For each test cases, output the minimum number of moves the configuration takes to reach the one shown inFigure 2. If this is impossible, just output"-1"And nothing else.

Sample Input

3132450624106530123456

Sample output

10-10


The slag level was resolved and the BFS was taken up. PS: It's too easy to write a blog, because the time is tight. Therefore, if you have enough time in the future, we should be fine-tuned.

Question: The minimum number of steps required to obtain the final state from the current state. You can only move =-= in spaces and search from the grinding status.

The location where the space is located and where the space can be moved. For example, if the space is at location 2, the positions 3, 0, and 1 can be moved to this location.

In addition, for me, it is the use of map in STL. The map <string, int> visit record has been accessed by wood, Map <string, int> ans records the minimum number of steps required for a specific State. Below is a drop code. Idea Source: Click to open the link

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h >#include <queue >#include <map> using namespace STD; struct node {char s [10]; int step ;}; Map <string, int> visit; map <string, int> ans; char S1 [10], S2 [10] = "0123456"; int Dr [8] [5] = {2, 4, 6, -1}, {,-1}, {, 0,-1}, {,-1}, {, 0,-1, -1}, {, 0,-1}; // record each position starting from 0. If it is 0, other positions that can be moved to this position are void BFS () {queue <node> q; node St, Ed; Str CPY (St. S, S2); ST. Step = 0; visit [s2] = 1; q. Push (ST); While (! Q. empty () {ST = Q. front (); q. pop (); ans [st. s] = ST. step; int temp; For (INT I = 0; I <7; I ++) // locate the 0 position {If (St. s [I] = '0') {temp = I; break ;}} for (INT I = 0; Dr [temp] [I]! =-1; I ++) // you can search {char ss [10]; strcpy (SS, St. s); SS [temp] = ST. s [Dr [temp] [I]; // Switch location ss [Dr [temp] [I] = '0'; If (! Visit [ss]) // {visit [ss] = 1; strcpy (ed. s, SS); ed. step = ST. step + 1; q. push (ed) ;}}} int main () {BFS (); int N; scanf ("% d", & N); While (n --) {getchar (); scanf ("% s", S1); If (! Strcmp (S1, S2) printf ("% d \ n", 0); else printf ("% d \ n", ANS [S1] = 0? -1: ANS [S1]);} return 0 ;}


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.