Luo Valley P2730 [IOI] Rubik's Magic Magic Squares

Topic background

After successfully invented cube, Mr. Rubik its two-dimensional version of the invention, called the magic plate. This is an 8 equally sized grid plate magic:

1 2 3 4

8 7 6 5

Title Description

We know that each square Rubik's Magic has a color. This 8 colors is represented by a positive integer before eight. The color sequence may be used to represent one state magic plate, starting from the upper left corner of a predetermined magic plate, sequentially in a clockwise direction taken an integer constituting a color sequence. For Rubik's Magic state diagram, we use the sequence (1,2,3,4,5,6,7,8), respectively. This is the basic state.

Here we provide three basic operations, respectively, with a capital letter "A", "B", "C" is represented (the state may be changed by these operations magic plate):

"A": exchanging upper and lower rows;

"B": will insert a leftmost rightmost;

"C": the central magic plate fourfold rotated clockwise.

The following is an exemplary basic operation state:

A: 8 7 6 5

1 2 3 4

B: 4 1 2 3

5 8 7 6

C: 1 7 2 4

8 6 3 5

For each possible state, these three basic operations can be used.

You want to calculate the programming operation is completed with minimum basic transition destination state to the basic state, the output of the basic sequence of operations.

Input Format

Only one line, comprising eight integers, separated by spaces (integers in the range 1--8) does not wrap, it represents the target state.

Output Format

Line 1: comprises an integer representing the length of the shortest sequence of operations.

Line 2: sequence of operations in the lexicographically earliest, by the string, except the last, output of 60 characters per line.

Sample input and output

Input # 1
2 6 8 4 5 7 3 1 
Output # 1
7 
BCABCCB

Description / Tips

Title Translation from NOCOW.

USACO Training Section 3.2

 

Parsing: ----- ----- BFS wide search

For state using the stored character string is used in which the eight pressed into a string of digital manner
, for example, an initial state is "12345678", and is stored as a character string "12348765."
Is then transformed according to the conversion rule three ABC
until it becomes the target state
noted that the second portion also inverted target state
such as "26845731" is stored as "26841375."

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<algorithm>
 7 #include<iomanip>
 8 #include<cstdlib>
 9 #include<queue>
10 #include<set>
11 #include<map>
12 #include<stack>
13 #include<vector>
14 #define LL long long
15 #define re register
16 #define Max 100001
17 struct MoBan {
18     std::string p;
19     std::string str;
20     int step;
21 };
22 std::queue<MoBan>q;
23 std::string D;
24 int ans;
25 std::map<std::string,int>m;
26 std::string BFS()
27 {
28     MoBan now,net;
29     while(!q.empty()) {
30         now=q.front();q.pop();
31         std::string str=now.str;
32         int t=now.step;
33         std::string p=now.p;
34         if(str==D) {
35             ans=t;
36             return p;
37             break;
38         }
39         ++t;
40         //A
41         std::string d="";
42         for(re int i = 4 ; i <= 7 ; ++ i) d+=str[i];
43         for(re int i = 0 ; i <= 3 ; ++ i) d+=str[i];
44         net.str=d;net.p=p+"A";
45         net.step=t;
46         if(m[d]!=1) q.push(net),m[d]=1;
47         //B
48         std::string a="";
49         a+=str[3];
50         for(re int i = 0 ; i < 3 ; ++ i) a+=str[i];
51         a+=str[7];
52         for(re int i = 4 ; i < 7 ; ++ i) a+=str[i];
53         net.str=a;net.p=p+"B";
54         if(m[a]!=1) q.push(net),m[a]=1;
55         //C
56         std::string c="";
57         c+=str[0],c+=str[5],c+=str[1],c+=str[3],c+=str[4],c+=str[6],c+=str[2],c+=str[7];
58         net.str=c;net.p=p+"C";
59         if(m[c]!=1) q.push(net),m[c]=1;
60     }
61 }
62 int main()
63 {
64     char ch[9];std::string str="12348765";
65     for(re int i = 1 ; i <= 8 ; ++ i) std::cin >> ch[i];
66     for(re int i = 1 ; i <= 4 ; ++ i) D+=ch[i];
67     for(re int i = 8 ; i >= 5 ; -- i) D+=ch[i];
68     MoBan now;m[str]=1;
69     now.p="";now.step=0;now.str=str;q.push(now);std::string p=BFS();
70     printf("%d\n",ans);
71     int len=p.length();std::cout << p[0];
72     for(re int i = 1 ; i < len ; ++ i) {
73         std::cout << p[i];
74         if(i%60==0) std::cout << '\n';
75     }
76     return 0;
77 }
AC Code

Guess you like

Origin www.cnblogs.com/handsomegodzilla/p/11287637.html