Sicily 2011. Nine Digits

Source: Internet
Author: User

. Nine Digits Constraints

Time limit:2 secs, Memory limit:256 MB

Description

Nine tiles, each with a number from 1 through 9 on it, is packed into a 3 by 3 frame. Your task is to arrange, the tiles so, they are ordered as:

1 2 34 5 67 8 9at each step, you can do the following operation to the Tiles:choose 2 by 2 tiles, rotate the tiles in clockwi SE order. For example:1 2 3 4 1 3 1 2 3 1 2 34 5 6 = 5 2 6 or 4 5 6 = 4 8 57 8 9 7 8 9 7 8 9 7 9 6

Write a program to find the minimum number of steps.

Input

Input contains multiple test cases.

Each test case is a description of a configuration of the nine tiles. The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and from Left-to-right within a row, where the tiles is represented by numbers 1 to 9. For example:9 8 76 5 43 2 1is described by this list:9 8 7 6 5 4 3 2 1

Output

Output the minimum number of steps on a single line for each test case.

Sample Input
1 2 3 4 5 6 7 8 94 1 3 5 2 6 7 8 9
Sample Output
03
Problem Source

Every Monday: 2010 Sun Yat-sen University Beginner's tournament


Quite a good problem, BFS + Cantor, the method is reverse thinking, the 123456789 is constantly transformed into various permutations, and record the number of steps;

But there is a problem, want to do so, you have to use the array to save the number of steps, the array is not open so large, it is necessary to use the Cantor, in fact, Cantor is the combination of permutations and real numbers to establish a mapping;

#include <iostream> #include <queue> #include <stdio.h>using namespace Std;int fac[9] = {40320, 5040,    720, 120, 24, 6, 2, 1, 1};//factorial value, int ans[362880 + 1];//for Cantor holds answer int make_num (int a[]) {//Converts a numeric array to a numeric int sum = 0;    for (int i = 0; i < 9; i++) {sum = sum * + a[i]; } return sum;}    void make_permutation (int pre, int a[], int pos) {//read into a number and then return the next change in the array int i = 8;        while (pre) {a[i--] = pre% 10;    Pre/= 10;        } if (pos = = 1) {int temp = a[0];        A[0] = a[1];        A[1] = a[4];        A[4] = a[3];        A[3] = temp;    Return        } else if (pos = = 2) {int temp = a[1];        A[1] = a[2];        A[2] = a[5];        A[5] = a[4];        A[4] = temp;    Return        } else if (pos = = 3) {int temp = a[3];        A[3] = a[4];        A[4] = a[7];        A[7] = a[6];        A[6] = temp;    Return        } else {int temp = a[4];        A[4] = a[5];        A[5] = a[8];      A[8] = a[7];  A[7] = temp;    Return    }}int Cantor (int a[]) {//Cantor int pos = 0, Count, I, J;        for (i = 0; i < 8; i++) {count = 0;        for (j = i + 1; j < 9; J + +) {if (A[i] > A[j]) count++;    } pos + = Count * Fac[i]; } return POS;}    void BFs () {queue<int> Q;    Q.push (123456789);    int step = 0;    int i, next[9];    int POS;        int now_size; while (!                Q.empty ()) {step++;//Each update is a one-step now_size = Q.size ();//Record the current size, and then go through it (and generate a new size)                 while (now_size--) {for (i = 1; I <= 4; i++) {//traversal of four conversion make_permutation (Q.front (), Next, I); pos = Cantor (next),//Get position via Cantor if (ans[pos] = = 0) {//If 0 is not used Ans[po                    s] = step;                Q.push (Make_num (next));    }} q.pop ();//Team first already used}} Ans[0] = 0;}    int main () {BFS ();    int sum[20]; while (~scaNF ("%d", &sum[0])) {for (int i = 1; I <= 8; i++) {scanf ("%d", &sum[i]);    } printf ("%d\n", Ans[cantor (sum)]);                   } return 0;}


Sicily 2011. Nine Digits

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.