3.2.E Magic Squares
Following the success of the magic cube, Mr. Rubik invented its planar version, called magic squares. This is a sheet composed of 8 equal-sized squares:
In this task we consider the version where each square has a different color. Colors are denoted by the first 8 positive integers. A sheet configuration is given by the sequence of colors obtained by reading the colors of the squares starting at the upper left corner and going in clockwise direction. For instance, the configuration of Figure 3 is given by the sequence (1,2,3,4,5,6,7,8). This configuration is the initial configuration. Three basic transformations, identified by the letters `A', `B' and `C', can be applied to a sheet:
Below is a demonstration of applying the transformations to the initial squares given above:
All possible configurations are available using the three basic transformations. You are to write a program that computes a minimal sequence of basic transformations that transforms the initial configuration above to a specific target configuration. PROGRAM NAME: msquareINPUT FORMATA single line with eight space-separated integers (a permutation of (1..8)) that are the target configuration. SAMPLE INPUT (file msquare.in)2 6 8 4 5 7 3 1 OUTPUT FORMAT
SAMPLE OUTPUT (file msquare.out)7 |
下附代碼:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
struct SQUARE{
int square[8+2];
string step;
}queue[40320*3], aim;
bool flag[40320+2];
int aim_num;
int front=0, back=0;
int tran(SQUARE Get){
int cnt = 0;
int pot;
for(int i = 0, tmp = 0; i<8; ++i){
pot = Get.square[i];
for(int j = 0; j<i; ++j) if(Get.square[j]<pot) ++tmp;
pot = pot-1-tmp;
tmp = 1;
for(int j = 8-i-1; j>1; --j) tmp *= j;
cnt += tmp*pot;
tmp = 0;
}
return cnt+1;
}
void Push(SQUARE Get){
for(int i = 0; i<8; ++i) queue[back].square[i] = Get.square[i];
queue[back].step = Get.step;
++back;
}
void Front(SQUARE &Send){
for(int i = 0; i<8; ++i) Send.square[i] = queue[front].square[i];
Send.step = queue[front].step;
++front;
}
int Strlen(string s){
int cnt = 0;
for(int i = 0; s[i]; ++i) ++cnt;
return cnt;
}
void output(SQUARE Get){
cout << Strlen(Get.step) << endl;
cout << Get.step << endl;
exit(0);
}
void A(SQUARE Get){
swap(Get.square[0], Get.square[7]);
swap(Get.square[1], Get.square[6]);
swap(Get.square[2], Get.square[5]);
swap(Get.square[3], Get.square[4]);
Get.step += "A";
if(tran(Get)==aim_num) output(Get);
if(back<40320*3) Push(Get);
}
void B(SQUARE Get){
swap(Get.square[0], Get.square[3]);
swap(Get.square[1], Get.square[3]);
swap(Get.square[2], Get.square[3]);
swap(Get.square[4], Get.square[5]);
swap(Get.square[5], Get.square[6]);
swap(Get.square[6], Get.square[7]);
Get.step += "B";
if(tran(Get)==aim_num) output(Get);
if(back<40320*3) Push(Get);
}
void C(SQUARE Get){
swap(Get.square[2], Get.square[5]);
swap(Get.square[2], Get.square[1]);
swap(Get.square[1], Get.square[6]);
Get.step += "C";
if(tran(Get)==aim_num) output(Get);
if(back<40320*3) Push(Get);
}
int main() {
freopen("msquare.in", "r", stdin); freopen("msquare.out", "w", stdout);
for(int i = 0; i<8; ++i) cin >> aim.square[i];
aim_num = tran(aim);
SQUARE Initial;
for(int i = 0; i<8; ++i) Initial.square[i] = i+1;
Push(Initial);
SQUARE tmp;
bool none = false;
while(front != back){
Front(tmp);
int tmp_num = tran(tmp);
if(tmp_num==aim_num) break;
if(flag[tmp_num])continue;
flag[tmp_num] = true;
A(tmp);
B(tmp);
C(tmp);
}
cout << 0 << endl << endl;
fclose(stdin); fclose(stdout); return 0;
}