Hdu1430 magic board (bidirectional BFS + output path + minimum Lexicographic Order)

Source: Internet
Author: User
Magic Board

Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1352 accepted submission (s): 270


Shortly after Problem description swept the globe, Mr. Rubik invented its simplified version, the 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 (3)


Recommendlinle

The Chinese question is not explained.

Question Analysis: This question is similar to octa digital. After reading the question and analyzing it, we found that there are nine States in each of the eight locations. Because the numbers are different, we decided to use Kanto.

So there are only 8! So we first took a simple BFs,

The result is Lily's TLE, because there are too many cases in the background .. If one direction is not good, then two directions. I just want to practice BFs,

Although this question only requires one-way BFS pre-processing, it can be very fast, but just to practice,

As a result, it took us two days to get to WAF. Last thanks @ fancymouse
Found the bug, and finally passed.

This two-way BFS is not that complicated, but the output path is a bit complicated. What's more, DT requires the smallest Lexicographic Order !!

The method is as follows. Forward search is common, and you can simply search for it and scale it in sequence by ABC. Flag [0] [State] records the starting point of forward search.

In a path, the op [0] [State] records the operations from the State to the State.

The reverse direction is almost the same. It expands from the end point to the middle. However, during expansion, the inverse operation of the abc3 operation is not performed in the ABC operation,

That is, the operation will still flip the string. The B operation is to move the row left to 1, and the C operation is the four left to 1 in the middle. What are the advantages of this operation,

We found that the output path should be searched for duplicate nodes in both positive and negative directions. In this way, we can directly scan the path from the encounter point to the end point,

The scanned path is the forward operation path. For example, if the two-step ca reversely expands from the endpoint to a second-to-last step,

The last and second steps can also reach the destination through normal AC operations. Therefore, OP [1] [State] records the reverse direction from the end point.

The inverse operation used to expand to the state is actually the forward operation from the state to the end is also op [1] [State],

Flag [1] [State] is the successor State of the recorded state to the final state, so as to ensure

The second half is the smallest Lexicographic Order (think about it, why ). However, every time a solution is found, the output is not in a hurry. Do not jump out of the loop,

Instead, we need to continue to make a comparison. This is why I cannot afford it,

Always wa, because there will be multiple solutions in the search process. For example, BCCC and ABCA in this question are operations with the same effect.

If the two solutions are aaabbb and bbbaaa, and the solution is found during reverse search, the reverse order ensures the minimum Lexicographic Order,

Therefore, in the reverse direction, it will first expand to AAA and find the solution.

Break is obviously faulty,

Obviously, this Lexicographic Order is not necessarily the smallest, and the results of the first half are not taken into account. So we

After finding the solution, do not jump out in a hurry. Just mark it, search the same layer, and select the smallest Lexicographic Order as the optimal solution.

Code writing is long, but clear.

For details, see the code:

# Include <iostream> # include <cstdio> # include <queue> # include <cstring> using namespace STD; const int n = 1000005; int flag [2] [50000]; char op [2] [50000]; long FAC [] = {24,120,720,504, 362880,}; struct node {int step, Val; char State [10];} SS, now, temp [N]; int tmpnum; struct que {struct node T [N]; int head, tail; void Init () {head = tail = 0 ;} bool empty () {return tail = head;} struct node top () {Return T [head];} void push (struct node A) {T [tail] = A; tail ++; If (tail> = N) tail % = N ;} void POP () {head ++; If (head> = N) Head % = n ;}} Q [2]; // queue <node> q [2]; int contor () {int I, j; long temp, num; num = 0; for (I = 0; I <8; I ++) {temp = 0; for (j = I + 1; j <8; j ++) if (ss. state [J] <ss. state [I]) temp ++; num + = temp * FAC [8-I-1];} return num;} void change (INT type) {int I; char C; swit CH (type) {Case 1: for (I = 0; I <4; I ++) {ss. state [I] ^ = ss. state [7-I]; SS. state [7-I] ^ = ss. state [I]; SS. state [I] ^ = ss. state [7-I];} break; Case 2: c = ss. state [3]; for (I = 3; I> 0; I --) ss. state [I] = ss. state [I-1]; SS. state [I] = C; C = ss. state [4]; for (I = 4; I <7; I ++) ss. state [I] = ss. state [I + 1]; SS. state [I] = C; break; Case 3: c = ss. state [1]; SS. state [1] = ss. state [6]; SS. state [6] = SS. state [5]; SS. state [5] = ss. state [2]; SS. state [2] = C; break;} void Change2 (INT type) {int I; char C; Switch (type) {Case 1: for (I = 0; I <4; I ++) {ss. state [I] ^ = ss. state [7-I]; SS. state [7-I] ^ = ss. state [I]; SS. state [I] ^ = ss. state [7-I];} break; Case 2: c = ss. state [0]; for (I = 0; I <3; I ++) ss. state [I] = ss. state [I + 1]; SS. state [I] = C; C = ss. state [7]; for (I = 7; I> 4; I --) ss. state [I] = Ss. state [I-1]; SS. state [I] = C; break; Case 3: c = ss. state [1]; SS. state [1] = ss. state [2]; SS. state [2] = ss. state [5]; SS. state [5] = ss. state [6]; SS. state [6] = C; break;} Char S1 [10], S2 [10]; int end, start; char ans [50000]; char tans [5000]; char ttans [5000]; int ansnum; void print () {int I; int root = ss. val; int num = 0; tans [num ++] = op [0] [ss. val]; while (root! = Start) {root = Flag [0] [root]; tans [num ++] = op [0] [root];} int p = 0; for (I = num-2; I> = 0; I --) {// printf ("% C", ANS [I]); ttans [p ++] = tans [I];} num = 0; root = ss. val; tans [num ++] = op [1] [ss. val]; while (root! = END) {root = Flag [1] [root]; tans [num ++] = op [1] [root];} for (I = 0; I <num-1; I ++) {// printf ("% C", ANS [I]); ttans [p ++] = tans [I];} ttans [p] = '\ 0'; If (ansnum) {If (strcmp (ANS, ttans)> 0) strcpy (ANS, ttans);} elsestrcpy (ANS, ttans); ansnum ++; // newans;} int main () {int I, j; while (~ Scanf ("% S % s", S1, S2) {If (strcmp (S1, S2) = 0) {puts (""); continue ;} Q [0]. init (); Q [1]. init (); // while (! Q [0]. Empty () // Q [0]. Pop (); // while (! Q [1]. empty () // Q [1]. pop (); memset (flag,-1, sizeof (FLAG); memset (OP, 0, sizeof (OP); SS. step = 0; strcpy (ss. state, S1); Start = contor (); SS. val = start; flag [0] [start] = start; Q [0]. push (SS); strcpy (ss. state, S2); End = contor (); SS. val = end; flag [1] [end] = end; Q [1]. push (SS); bool OK = false; I = 0; tmpnum = 0; ansnum = 0; while (! OK) {While (! Q [0]. empty () & Q [0]. top (). step = I) {now = Q [0]. top (); Q [0]. pop (); SS = now; SS. step ++; change (1); SS. val = contor (); If (flag [0] [ss. val] =-1) {flag [0] [ss. val] = now. val; OP [0] [ss. val] = 'a'; If (flag [1] [ss. val]! =-1) {print (); OK = true; // break; // evil break} Q [0]. push (SS) ;}ss = now; SS. step ++; change (2); SS. val = contor (); If (flag [0] [ss. val] =-1) {flag [0] [ss. val] = now. val; OP [0] [ss. val] = 'B'; If (flag [1] [ss. val]! =-1) {print (); OK = true; // break; // source of WA} Q [0]. push (SS) ;}ss = now; SS. step ++; change (3); SS. val = contor (); If (flag [0] [ss. val] =-1) {flag [0] [ss. val] = now. val; OP [0] [ss. val] = 'C'; If (flag [1] [ss. val]! =-1) {print (); OK = true; // break; // analyze calmly} Q [0]. push (SS) ;}} if (OK) break; while (! Q [1]. empty () & Q [1]. top (). step = I) {// release the same layer together and expand by ABC at the same time to ensure reverse temp [tmpnum ++] = Q [1]. top (); // minimum Lexicographic Order, Q [1]. pop (); // if you cannot understand it, draw a picture on the paper with a pen.} For (j = 0; j <tmpnum; j ++) {Ss = temp [J]; SS. step ++; Change2 (1); SS. val = contor (); If (flag [1] [ss. val] =-1) {flag [1] [ss. val] = temp [J]. val; OP [1] [ss. val] = 'a'; If (flag [0] [ss. val]! =-1) {print (); OK = true; // break requires three thoughts} Q [1]. push (SS) ;}}for (j = 0; j <tmpnum; j ++) {Ss = temp [J]; SS. step ++; Change2 (2); SS. val = contor (); If (flag [1] [ss. val] =-1) {flag [1] [ss. val] = temp [J]. val; OP [1] [ss. val] = 'B'; If (flag [0] [ss. val]! =-1) {print (); OK = true; // break; // unexpected} Q [1]. push (SS) ;}}for (j = 0; j <tmpnum; j ++) {Ss = temp [J]; SS. step ++; Change2 (3); SS. val = contor (); If (flag [1] [ss. val] =-1) {flag [1] [ss. val] = temp [J]. val; OP [1] [ss. val] = 'C'; If (flag [0] [ss. val]! =-1) {print (); OK = true; // break ;//....} Q [1]. push (SS) ;}} tmpnum = 0; I ++; If (OK) break;} printf ("% s \ n", ANS) ;}return 0 ;} // 1234ms820k // 625ms924k okay. The submission time of G ++ is doubled... /X 12345678645728131234567874381652158746237384165212345678827546311234567848136275123456781234567812345678876543211234567841236785123456785876321412345678863542711234567858632714123456785186372412345678427368151763825446273185762538412638715413578642246875313671825482716435123456782134567846753812543876123752168454137628 */

What happened to csdn layout ....

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.