HDU 1430 magic board (preprocessing + Replacement + BFS)

Source: Internet
Author: User
Magic Board

Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)

Total submission (s): 1401 accepted submission (s): 288 Problem description soon after the cube swept the globe, Mr. Rubik invented its simplified version-magic board. The magic board consists of eight blocks of the same size, each of which has different colors and can be represented by numbers 1-8. At any time, the status of the magic board can be represented by the color sequence of the Square: from the upper left corner of the magic board, write the color code of each square in clockwise order, the sequence of digits indicates the status of the magic board. For example, the sequence (,) indicates that the magic board status is:

1 2 3 4
8 7 6 5

Three different operations can be applied to the magic board:

A: swap the upper and lower lines, for example, change to status 87654321.
B: Shifts one cell to the right of each row at the same time, for example, 41236785.
C: four boxes in the middle rotate one grid clockwise. For example, the variable is changed to 17245368.

For the initial and target statuses of the magic board, please give the transformation steps from the initial state to the smallest number of changes in the object state. If there are multiple conversion schemes, take the smallest Lexicographic Order.

Each group of input test data includes two rows, representing the initial and object states of the magic board.

Output converts the output of each group of test data to meet the requirements of the question.

Sample Input

12345678172453681234567882754631
 

Sample output

CAC
 

Authorll

Sourceacm summer training team exercise session (3) feeling: with this question, I decided to use a single BFs, and it turned out to be TLE. Later, I found multiple groups of data tests. If there are many background cases, I can drag the program to TLE. If it is a single data group test, a single search is certainly acceptable, because there are not many States. Later, we changed the two-way BFS and kept wa. In fact, the two-way BFS is a pitfall. It is really difficult to ensure the smallest Lexicographic Order of the second half, but it is not impossible. I felt that my double search was not easy to change. I took a look at the online pre-processing + replacement, and it was quite classic. This method works well for multiple groups of data tests.
Idea: although the initial State S is not 12345678, it can be regarded as 12345678 (State SS), so there is a corresponding relationship. Replace the final State E with this corresponding relationship to get the State es, then S-> E can be equivalent to SS-> es. In this way, you only need to perform a BFS preprocessing on the SS status and save the answer to OK.
Code:

# Include <iostream> # include <cstdio> # include <string> # include <cstring> # define maxn 50000 using namespace STD; int n, m; int FAC [] = {24,120,720,504, 0}; // expand char s [10], E [10]; bool vis [maxn]; string anss [maxn]; // Save the answer struct node {int state; char NS [10];} cur, now, Q [maxn]; void getns (int K) // obtain the magic sequence {int I, j; If (k = 0) {for (I = 0; I <4; I ++) {cur. NS [3-I] = now. NS [4 + I]; cur. NS [4 + I] = now. NS [3-I] ;}} else if (K = 1) {for (I = 0; I <3; I ++) {cur. NS [I + 1] = now. NS [I]; cur. NS [I + 4] = now. NS [I + 5];} cur. NS [0] = now. NS [3]; cur. NS [7] = now. NS [4];} else {for (I = 0; I <8; I ++) {cur. NS [I] = now. NS [I];} cur. NS [1] = now. NS [6]; cur. NS [2] = now. NS [1]; cur. NS [5] = now. NS [2]; cur. NS [6] = now. NS [5] ;}} int getstate (char ss []) // obtain the status {int I, j, CNT, val = 0; for (I = 0; I <8; I ++) {CNT = 0; For (j = I + 1; j <8; j ++) {If (ss [J] <ss [I]) CNT ++;} Val + = CNT * FAC [7-i];} Return val;} void BFS () {int I, j, NST, TST; int head = 0, tail =-1; char C; memset (VIS, 0, sizeof (VIS); cur. state = 0; strcpy (cur. NS, "12345678"); vis [0] = 1; anss [0] = ""; Q [++ tail] = cur; while (Head <= tail) {now = Q [head]; NST = now. state; for (I = 0; I <3; I ++) {getns (I); TST = getstate (cur. NS); If (! Vis [TST]) {vis [TST] = 1; C = 'A' + I; anss [TST] = anss [NST] + C; cur. state = TST; Q [++ tail] = cur ;}} head ++ ;}int main () {int I, j, est; char CS [10]; BFS (); // preprocessing while (~ Scanf ("% S % s", S, E) {for (I = 0; I <8; I ++) // establish the ing {CS [s [I]-'0'] = I + 1 + '0';} for (I = 0; I <8; I ++) // replace {e [I] = cs [E [I]-'0'];} est = getstate (E ); cout <anss [est] <Endl;} 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.