【USACO 3.2】(msquare)Magic Squares
-----------Prob-------------
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.
-----------solution------------
看到这么渣渣的范围我以为自己看错了。。。但一想到USACO总是一道牛题+一道水题的循环,就释然了
直接搜吧,状态量很小,把每种状态记成一个8位数就可以了
--------------Code--------------
/*
ID:zst_0111
TASK:msquare
LANG:C++
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
bool flag[8][8][8][8][8][8][8];
int a[9];
int sta[9];
int to[9];
char output[501];
int q[100001][3];
bool check()
{
return !flag[to[1]][to[2]][to[3]][to[4]][to[5]][to[6]][to[7]]?
flag[to[1]][to[2]][to[3]][to[4]][to[5]][to[6]][to[7]]=true:
false;
}
int main()
{
int i,j,k,m,n;
freopen("msquare.in","r",stdin);
freopen("msquare.out","w",stdout);
int target=0;
for(i=1;i<=4;i++)
scanf("%d",&a[i]);
for(i=8;i>=5;i--)
scanf("%d",&a[i]);
for(i=1;i<=8;i++)
target*=10,target+=a[i];
q[1][0]=12348765;
if(q[1][0]==target)
{
printf("0\n\n");
return 0;
}
int s=0,t=1;
while(s<t)
{
s++;
int now=q[s][0];
for(i=1;i<=8;i++)
sta[8-i+1]=now%10,now/=10;
to[1]=sta[5];to[5]=sta[1];
to[2]=sta[6];to[6]=sta[2];
to[3]=sta[7];to[7]=sta[3];
to[4]=sta[8];to[8]=sta[4];
if(check())
{
m=0;
for(i=1;i<=8;i++)
m*=10,m+=to[i];
q[++t][0]=m;
q[t][1]=s;
q[t][2]=0;
}
if(m==target)
break;
to[1]=sta[4];to[5]=sta[8];
to[2]=sta[1];to[6]=sta[5];
to[3]=sta[2];to[7]=sta[6];
to[4]=sta[3];to[8]=sta[7];
if(check())
{
m=0;
for(i=1;i<=8;i++)
m*=10,m+=to[i];
q[++t][0]=m;
q[t][1]=s;
q[t][2]=1;
}
if(m==target)
break;
to[1]=sta[1];to[5]=sta[5];
to[2]=sta[6];to[6]=sta[7];
to[3]=sta[2];to[7]=sta[3];
to[4]=sta[4];to[8]=sta[8];
if(check())
{
m=0;
for(i=1;i<=8;i++)
m*=10,m+=to[i];
q[++t][0]=m;
q[t][1]=s;
q[t][2]=2;
}
if(m==target)
break;
}
n=0;
k=t;
while(k!=1)
output[++n]=q[k][2]+'A',k=q[k][1];
printf("%d\n",n);
int calc=0;
for(i=n;i>=1;i--)
{
putchar(output[i]);
++calc;
if(calc%60==0)
printf("\n");
}
printf("\n");
return 0;
}