Shortly after problem Description the Rubik's Cube, Mr. Rubik invented its simplified version, the Magic board. The Magic board is made up of 8 blocks of the same size, each with a different color, with the number 1-8 being used respectively. 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 clockwise direction, and the resulting number sequence can represent 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, the specific operation method is as follows:
A: The upper and lower lines are interchangeable, as above can be transformed to state 87654321
B: Each line loops right one cell at the same time, as above can be transformed to 41236785
C: The middle 4 blocks clockwise rotation of a lattice, as above 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.
Input each set of test data includes two lines, respectively, representing the initial state and the state of the magic Plate.
Output satisfies the test instructions transformation step for each set of test data outputs.
Sample Input
12345678 17245368 12345678 82754631
Sample Output
C AC
Author LL
The first Kang expansion of the problem, because the second line of Magic board, because it is reversed, so I deal with it as a pendulum
That is
1234
8765
And when I'm dealing with it,
1234
5678
Comtop expansion of the principle of postgraduate examination here: http://blog.csdn.net/zhongkeli/article/details/6966805
Once you know the principle, it's not hard to solve.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Queue> using namespace std;
String start,end,ans[50000];
int hash[10],pos[10],vis[50000];
struct Node {string step,str;
int Val;
};
int solve (string &s) {int i,j,sum = 0;
for (i = 0; i<7; i++) {int cnt = 0;
for (j = i+1; j<8; j + +) if (s[i]>s[j]) cnt++;
Sum+=cnt*hash[7-i];
} return sum;
} void Fun_a (String &s) {for (int i = 0; i<4; i++) swap (s[i],s[i+4]);}
void Fun_b (String &s) {char t=s[3];
for (int i=2; i>=0; i--) s[i+1]=s[i];
s[0]=t;
T=S[7];
for (int i=6; i>=4; i--) s[i+1]=s[i];
s[4]=t;
} void Fun_c (String &s) {char t=s[1];
S[1]=S[5];
S[5]=S[6];
S[6]=S[2];
s[2]=t;
} void BFs () {memset (vis,0,sizeof (VIS));
Node A,next;
Queue<node> Q;
A.step = ""; A.str= start;
A.val = Solve (start);
Vis[a.val] = 1;
Ans[a.val] = "";
Q.push (a); while (!
Q.empty ()) {a = Q.front ();
Q.pop ();
String T;
int k;
t = a.str;
Fun_a (t);
K = Solve (t);
while (!vis[k]) {vis[k] = 1;
Next = A;
next.step+= ' A ';
Ans[k] = Next.step;
Next.str = t;
Next.val = k;
Q.push (next);
} t = A.str;
Fun_b (t);
K = Solve (t);
while (!vis[k]) {vis[k] = 1;
Next = A;
next.step+= ' B ';
Ans[k] = Next.step;
Next.str = t;
Next.val = k;
Q.push (next);
} t = A.str;
Fun_c (t);
K = Solve (t);
while (!vis[k]) {vis[k] = 1;
Next = A;
next.step+= ' C ';
Ans[k] = Next.step;
Next.str = t;Next.val = k;
Q.push (next);
}}} int main () {int i,j;
Hash[0] = 1;
for (i = 1; i<10; i++) hash[i] = Hash[i-1]*i;
Start = "12345678";
BFS ();
while (cin>>start>>end) {swap (start[4],start[7]);//Turn the Magic board into the condition I am dealing with (start[6],start[5]);
Swap (end[4],end[7]);
Swap (end[6],end[5]);
for (i = 0; i<8; i++) pos[start[i]-' 0 '] = i+1;
for (i = 0; i<8; i++) end[i] = pos[end[i]-' 0 '];
int k;
K = Solve (end);
cout << ans[k] << Endl;
} return 0;
}