/*
ID: niepeng1
LANG: C++
TASK:msquare
*/
/*
Magic SquaresIOI'96
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:
1 | 2 | 3 | 4 |
8 | 7 | 6 | 5 |
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:
- 'A': exchange the top and bottom row,
- 'B': single right circular shifting of the rectangle,
- 'C': single clockwise rotation of the middle four squares.
Below is a demonstration of applying the transformations to the initial squares given above:
A: |
| B: |
| C: |
|
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.
此题的康托展开很巧妙,可以参考学习下
http://baike.baidu.com/view/437641.htm?fr=ala0
*/
#include <iostream>
#define Max1 405000
//#define Max2 987654321
using namespace std;
FILE *in,*out;
unsigned long int dest,fro;
struct Node{
unsigned long num;
int pre;
int count;
int use;
};
Node way[Max1];
bool cond[405000]={false};
int fac[]={0,1,2,6,24,120,720,5040,40320,362880};//...
unsigned long int control(unsigned long int xx)
{
int n=8;
int i,j,temp,num;
unsigned long int tem2,tem1;
int s[8];
for(i=7,tem2=0xf0000000;i>=0;i--)
{
tem1=xx&tem2;
s[7-i]=tem1>>(i*4);
tem2=tem2>>4;
}
num=0;
for(i=0;i<n;i++)
{
temp=0;
for(j=i+1;j<n;j++)
{
if(s[j]<s[i]) temp++;
}
num+=fac[n-i-1]*temp;
}
return (num+1);
}
void read()
{
int i;
unsigned long int tem,tem1;
fro=1;
for(i=2;i<5;i++){
fro<<=4;
fro|=i;
}
for(i=8;i>4;i--)
{
fro<<=4;
fro|=i;
}
fscanf(in,"%d",&tem);
dest=tem;
for(i=2;i<5;i++){
fscanf(in,"%d",&tem);
dest<<=4;
dest|=tem;
}
dest=dest<<16;
for(i=8,tem1=0;i>4;i--,tem1+=4)
{
fscanf(in,"%d",&tem);
dest+=(tem<<tem1);
}
}
void Disp(int num)
{
if( num==0 )
return;
Disp(way[num].pre);
switch(way[num].use)
{
case 1:
fprintf(out,"A");break;
case 2:
fprintf(out,"B");break;
default:
fprintf(out,"C");
}
}
void find()
{
int start,end;
unsigned long tem1,tem2,tem3,tem4,tem5,tem6;
start=0;end=0;
way[start].num=fro;
way[start].count=0;
way[start].pre=-1;
cond[control(fro)]=true;
if(fro==dest)
{
fprintf(out,"0/n/n");
return;
}
while(start<=end)
{
{
tem1=way[start].num&0x0000ffff;
tem2=way[start].num&0xffff0000;
tem1=(tem1<<16)+(tem2>>16);
if(!cond[control(tem1)])
{
cond[control(tem1)]=true;
end++;
way[end].num=tem1;
way[end].count=way[start].count+1;
way[end].pre=start;
way[end].use=1;
if(tem1==dest){
fprintf(out,"%d/n",way[end].count);
Disp(end);
fprintf(out,"/n");
return;
}
}
}
{
tem1=way[start].num&0x0000ffff;
tem2=way[start].num&0xffff0000;
tem3=(tem1&0x0000000f)<<12;
tem1=tem1>>4;
tem1=tem1+tem3;
tem3=(tem2&0x000f0000)<<12;
tem2=(tem2>>4)&0xffff0000;
tem2=tem2+tem3;
tem1=tem1+tem2;
if(!cond[control(tem1)])
{
cond[control(tem1)]=true;
end++;
way[end].num=tem1;
way[end].count=way[start].count+1;
way[end].pre=start;
way[end].use=2;
if(tem1==dest){
fprintf(out,"%d/n",way[end].count);
Disp(end);
fprintf(out,"/n");
return;
}
}
}
{
tem1=way[start].num&0x0ff00ff0;
tem2=way[start].num&0xf00ff00f;
tem3=(tem1&0x0f000000)>>4;
tem4=(tem1&0x00f00000)>>16;
tem5=(tem1&0x000000f0)<<4;
tem6=(tem1&0x00000f00)<<16;
tem1=tem2+tem3+tem4+tem5+tem6;
if(!cond[control(tem1)])
{
cond[control(tem1)]=true;
end++;
way[end].num=tem1;
way[end].count=way[start].count+1;
way[end].pre=start;
way[end].use=3;
if(tem1==dest){
fprintf(out,"%d/n",way[end].count);
Disp(end);
fprintf(out,"/n");
return;
}
}
}
start++;
}
}
int main()
{
in=fopen("msquare.in","r");
out=fopen("msquare.out","w");
read();
control(fro);
find();
fclose(in);
fclose(out);
return 0;
}