Magic BoardTime limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1679 Accepted Submission (s): 354
Shortly after problem description the Rubik's Cube, Mr. Rubik invented its simplified version, the Magic board. The Magic Plate is made up of 8 blocks of the same size, each with a different color, and can be represented by the number 1-8. At any moment the state of the magic Plate can be represented by the color sequence of the block: starting from the upper left corner of the magic Plate, the color code of each block is written in a clockwise direction, and the resulting number sequence represents the state of the magic Plate. For example, the sequence (1,2,3,4,5,6,7,8) indicates that the Magic plate State is:
1 2 3 4
8 7 6 5
For the Magic plate, you can apply three different operations, detailed operation methods such as the following:
A: The upper and lower lines are interchangeable, such as can be transformed to state 87654321
B: Loop Right one cell at the same time in each line, as can be transformed to 41236785
C: The middle 4 blocks clockwise rotation of a lattice, such as can be transformed to 17245368
Give you the original state of the magic plate and the target State, please give a minimum number of transformation steps from the initial state to the head, if there are many transformation schemes to take the smallest dictionary order.
The test data of input contains two lines, representing the initial state and the state of the magic Plate respectively.
Output satisfies the test instructions transformation step for each set of test data outputs.
Sample Input
12345678172453681234567882754631
Sample Output
Cac
Authorll SOURCEACM Summer Training Team Practice Competition (III)title: http://acm.hdu.edu.cn/showproblem.php?pid=1430
to do this problem, first of all to understand the development of Cantor, specific explanation can be stamped →http://blog.csdn.net/lttree/article/details/24798653I just started with BFS, TLE ... later switched to two-way BFS, found wa ... (My bi-directional bfs:http://blog.csdn.net/lttree/article/details/24811373)anyway, it's not solved at all, it seems to be a dictionary order problem.
look at the other people on the internet pretreatment. (Just play the Watch)the problem can then be solved by permutation: from one state to another, it can be converted from the starting point to a state.indefinitely feeling, later see the mountains and waters of the blog understand:
For (i=0;i<8;i++)
pos[s1[i]-' 0 ']=i+1+ ' 0 ';
For (i=0;i<8;i++)
s2[i]=pos[s2[i]-' 0 '];
K=kangtuo (S2);
The above section of code means: The starting target is regarded as 1,2,3,4,5,6,7,8;
Column: Position: 12345678 12345678
at first: 63728145 change 12345678
End point: 86,372,541 51234876
Explain: At the beginning: 6 in the 1th position, then find 6 in the end point with 1 substitution, 3 in the 2nd bit, in the end of the 3 replace with 2, and so on.
At the beginning we will first press 12345678 in this order to establish a tree-like, assuming that directly from the initial state does not change, then our results may have a lot of way, it is possible to go first A or B can reach the target, there are many roads, but first walked the path of B, will need to output small is the way from the beginning of a, then how to do it, you can use the idea of transformation, the initial state into 12345678, such words, we start from this order from the calculation!! Therefore, the conversion must be done first, on the target to find and write down the path, has been found finally parent node: 12345678.
/****************************************************************************** Author:Tree **From : Http://blog.csdn.net/lttree * * Title: Magic Board **SOURCE:HDU 1430 * * Hint: Cantor Expand BFS ******************************************************************************/#include <iostream > #include <string.h> #include <string> #include <queue>using namespace std;struct node{string str, Step;}; BOOL Vis[40320+1];int pos[10],fac[] = {1,1,2,6,24,120,720,5040,40320};//ans save answer from start point to this point string ans[50000];//cantor expand int Kangtuo (String a) {int i,j,t,sum; sum=0; for (i=0; i<8; ++i) {t=0; for (J=I+1;J<8;++J) if (a[i]>a[j]) ++t; SUM+=T*FAC[8-I-1]; } return sum+1;} Press A to transform void Move_a (string &s) {for (int i=0;i<4;++i) swap (s[i],s[i+4]);} Press B to transform string Move_b (string s) {string temp=s; int i; for (i=0;i<8;++i) {if (i==0 | | i==4) TEMP[I]=S[I+3]; else temp[i]=s[i-1]; } return temp;} Press C to transform void Move_c (string &s) {swap (s[1],s[2]); Swap (s[5],s[6]); Swap (s[1],s[6]);} void BFs (string s) {memset (vis,0,sizeof (VIS)); Queue <Node> q; Node Pre,lst; Pre.str=s; Pre.step= ""; Vis[kangtuo (s)]=1; Ans[kangtuo (s)]=pre.step; Q.push (pre); while (!q.empty ()) {Pre=q.front (); Q.pop (); Lst=pre; Move_a (LST.STR); if (!vis[kangtuo (LST.STR))) {lst.step+= "A"; Vis[kangtuo (LST.STR)]=1; Ans[kangtuo (LST.STR)]=lst.step; Q.push (LST); } lst.str=move_b (PRE.STR); if (!vis[kangtuo (LST.STR)]) {lst.step=pre.step+ "B"; Vis[kangtuo (LST.STR)]=1; Ans[kangtuo (LST.STR)]=lst.step; Q.push (LST); } Lst=pre; Move_c (LST.STR); if (!vis[kangtuo (LST.STR)]) { lst.step+= "C"; Vis[kangtuo (LST.STR)]=1; Ans[kangtuo (LST.STR)]=lst.step; Q.push (LST); }}}int Main () {int i,k; String s1,s2; preprocessing, from the starting point to the point. BFS ("12345678"); while (CIN>>S1>>S2) {//Change Order over//subject 12345678//In fact 1234//8765 We will store swap according to 12348765来 (S1[4],s1[7]); Swap (s1[5],s1[6]); Swap (s2[4],s2[7]); Swap (s2[5],s2[6]); for (i=0;i<8;i++) pos[s1[i]-' 0 ']=i+1; for (i=0;i<8;i++) s2[i]=pos[s2[i]-' 0 '];/* replacement */K=kangtuo (S2); cout<<ans[k]<<endl; } return 0;}
acm-Cantor expansion + pre-treatment of BFS--hdu1430