IOI ' The Magic square problem Solving report

Source: Internet
Author: User

IOI ' The Magic Square (Magic version)

The topic is translated by Jerryxie.

Magic Square (Msqaure.cpp/c/pas)

"Problem description"

After the success of the Rubik's Cube, Mr. Lubic invented its two-dimensional version, called the Magic version. It is a table consisting of eight blocks.

In the subject we think that each block has a different color, and these colors will be represented by a total of 8 integers in a 1~8. The state of a table is represented by a sequence of colors, given in a clockwise direction from the upper-left corner of the table. For example, the above table, the sequence is (1,2,3,4,5,6,7,8), which is called the initial state.

The Magic version has three basic transformations, with ' A ', ' B ', ' C ', can change the state of the magic version.

A: Exchange the first and second lines;

B: Insert the rightmost column to the far left;

C: Rotate the middle two columns clockwise by 90 degrees.

Here are three variations of the initial state:

All possible states can use these three basic transformations.

You need to write a program that calculates the shortest sequence of operations required to change from the initial state to the specified state.

Input

The input file name is msquare.in.

The input file has only one row, a total of 8 numbers, each separated by a space, and a color sequence for the specified state (denoted by 1~8).

Output

The output file name is Msquare.out.

Line 1:: An integer that represents the length of the shortest sequence of operations.

Line 2: Is the earliest sequence of operations in the dictionary sequence. Each line is 60 characters apart from the last line.

"Input and Output sample"

Msquare.in

Msquare.out

2 6 8 4 5 7 3 1

7

Bcabccb

"Problem Analysis"

Analysis of the problem, we can find that this can be completely solved with BFS. It also requires the first sequence of the output dictionary order so that we can exit the search by the BFS search to the first solution. Search method is: Starting from the initial state, respectively a,b,c three operations to get three states and then into the queue, and then take a state from the team head to repeat the operation, and finally find the solution. The length of the sequence of operations is the depth of the solution's nodes in the solution tree (the root node is the initial state depth of 0). The sentence weight can be used to expand processing, with a marker array to mark whether this state has been searched, if the search is not queued, in the process of the queue we need to record the current operation and the parent node, to facilitate the return of the output sequence of operations. Also, we find that there may be more than one line from Line2 in the output format, and after testing we find that the longest sequence of operations has more than 20 operations, so the code given below does not involve a 60-character line-wrapping problem, which can be supplemented in the output location if needed.

"Program source Code"

  1. Msquare.cpp by Jerryxie
  2. #include <cstdio>
  3. using namespace Std;
  4. struct queue//define struct to represent queue
  5. {
  6. int cantor,pre;
  7. Char ope;
  8. }Q[50001];
  9. int a[10],direct[10],bin[11],original[10];
  10. BOOL had[50001]; Weight tag Array
  11. void operation (char c)//perform a~c three operations
  12. {
  13. int i,j,t;
  14. if (c== ' A ')
  15. for (i=1,j=8;i<j;i++,j--)
  16. {
  17. T=a[i];
  18. A[I]=A[J];
  19. a[j]=t;
  20. }
  21. else if (c== ' B ')
  22. {
  23. T=A[4];
  24. for (i=4;i>=2;i--)
  25. A[I]=A[I-1];
  26. a[1]=t;
  27. T=A[5];
  28. for (i=5;i<=7;i++)
  29. A[I]=A[I+1];
  30. a[8]=t;
  31. }
  32. else if (c== ' C ')
  33. {
  34. T=A[3];
  35. A[3]=A[2];
  36. A[2]=A[7];
  37. A[7]=A[6];
  38. a[6]=t;
  39. }
  40. Else
  41. }
  42. void Create ()//Initialize initial state and factorial value of 0~10
  43. {
  44. int i;
  45. for (i=1;i<=8;i++)
  46. A[i]=i;
  47. Bin[0]=1;
  48. for (i=1;i<=10;i++)
  49. Bin[i]=bin[i-1]*i;
  50. Return
  51. }
  52. int xtonum (int x[])//shift the arrangement to a value (Cantor unfold)
  53. {
  54. int b[10]={0},i,j,num=0;
  55. for (i=1;i<=8;i++)
  56. for (j=i+1;j<=8;j++)
  57. if (X[i]>x[j])
  58. b[i]++;
  59. for (i=1;i<=8;i++)
  60. Num+=b[i]*bin[8-i];
  61. return num;
  62. }
  63. void translate (int b[])//convert the computed arrangement to the original arrangement
  64. {
  65. int i,j,s;
  66. BOOL T[10];
  67. for (i=1;i<=8;i++)
  68. T[i]=true;
  69. a[1]=b[1]+1;
  70. T[a[1]]=false;
  71. for (i=2;i<=8;i++)
  72. {
  73. s=0;
  74. for (j=1;j<=8;j++)
  75. if (t[j]==true)
  76. {
  77. s++;
  78. if (s==b[i]+1)
  79. {
  80. A[i]=j;
  81. T[j]=false;
  82. Break
  83. }
  84. }
  85. }
  86. }
  87. void numtoa (int num)//Convert value to arrangement (Inverse Cantor expansion)
  88. {
  89. int i,yushu,div,temp=num,b[10];
  90. for (i=1;i<=8;i++)
  91. {
  92. Div=temp/bin[8-i];
  93. Yushu=temp%bin[8-i];
  94. B[i]=div;
  95. Temp=yushu;
  96. }
  97. b[8]=0;
  98. Translate (b);
  99. Return
  100. }
  101. BOOL Ishad (int num)//weight, determine whether there is a current value in the queue, if any, is considered to have expanded this node, do not need to be queued for expansion
  102. {
  103. if (had[num]==true)
  104. return true;
  105. Else
  106. return false;
  107. }
  108. void print (int qnail)//Output solution
  109. {
  110. Char o[50001],i,onum=0;
  111. Queue T=q[qnail];
  112. while (T.PRE!=-1)//reverse-pushes the root node from the solution's node to record the middle operation
  113. {
  114. O[++onum]=t.ope;
  115. T=q[t.pre];
  116. }
  117. printf ("%d\n", onum); Output sequence length
  118. for (i=onum;i>=1;i--)//output operation sequence, since the record is pushed back to the root node from the solution's node, the output should also be reversed, representing the sequence of operations from the root node to the solution node
  119. printf ("%c", O[i]);
  120. printf ("\ n"); Line break (Usaco requires that the output file should have a blank line at the end)
  121. Return
  122. }
  123. int main ()
  124. {
  125. Freopen ("Msquare.in", "R", stdin);
  126. Freopen ("Msquare.out", "w", stdout);
  127. int i,final,nowcantor,qhead=1,qnail=0; Initialize team head and end of team position
  128. BOOL Sign=true; Search tags
  129. char c;
  130. Create (); Initialization
  131. for (i=1;i<=8;i++)//read into the target State
  132. scanf ("%d", &direct[i]);
  133. Final=xtonum (direct); To calculate the Cantor expansion value of the target State
  134. Nowcantor=xtonum (a); To calculate the initial state of the Cantor expansion value
  135. if (nowcantor==final)//If the target State is the initial state
  136. {
  137. printf ("0\n\n"); Output 0, because the operation sequence length is 0, so a row is empty
  138. Sign=false; Change the search flag, then do not need to search
  139. }
  140. else//If not, join the queue
  141. {
  142. Q[++qnail].cantor=nowcantor;
  143. Q[qnail].pre=-1; The parent node is-1
  144. q[qnail].ope=0; No action
  145. }
  146. while (sign)//Start Search
  147. {
  148. Numtoa (Q[qhead].cantor); Extract the Cantor of the team head expand value and revert to state
  149. for (i=1;i<=8;i++)//Save current state
  150. Original[i]=a[i];
  151. for (c=65;c<=67;c++)//from A~c cycle (ASCII value of ' a ' is 67, ASCII value of ' C ')
  152. {
  153. Operation (c); Basic transformations from A~c in turn
  154. Nowcantor=xtonum (a); Record the transformed Cantor expansion value
  155. if (nowcantor==final)//If the transformation is the same as the target State
  156. {
  157. Q[++qnail].cantor=nowcantor; Team
  158. Q[qnail].ope=c;
  159. Q[qnail].pre=qhead;
  160. Had[nowcantor]=true; Record already in use
  161. Print (Qnail); Output results
  162. Sign=false; Modify the search flag to exit the search
  163. Break Exit Current Loop
  164. }
  165. else//if not the same
  166. {
  167. if (Ishad (nowcantor) ==false)//If it has not been previously extended
  168. {
  169. Q[++qnail].cantor=nowcantor; Team
  170. Q[qnail].ope=c; Records the transformation actions used by the parent node transformation to the current state
  171. Q[qnail].pre=qhead; Record the location of the parent node
  172. Had[nowcantor]=true; Record already in use
  173. }
  174. }
  175. if (c== ' a ' | | c== ' B ')//If the transform operation is a~b, the A array reverts to the state before the transition to the next operation
  176. for (i=1;i<=8;i++)
  177. A[i]=original[i];
  178. }
  179. qhead++; Extended after the team
  180. }
  181. return 0;
  182. }

This procedure is tested and passed on Usaco.

Welcome contempt.

IOI ' The Magic square problem Solving report

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.