USACO SECTION 3.2 Magic Squares

Magic Squares
IOI'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:

1234
8765

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:
8765
1234
B:
4123
5876
C:
1724
8635

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: msquare

INPUT FORMAT

A 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

Line 1:A single integer that is the length of the shortest transformation sequence.
Line 2:The lexically earliest string of transformations expressed as a string of characters, 60 per line except possibly the last line.

SAMPLE OUTPUT (file msquare.out)

7
BCABCCB

 

/*
ID: conicoc1
LANG: C
TASK: msquare
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

FILE *fin,*fout;
int Queue[40320];
int front=0,rear=0;
int Goal[9];
int Square[9];
int TmpSquare[9];
int Dist[40320];    //标记是否出现过 
int Path[40320][2];



int Contor()
{
	int temp[8]={5040,720,120,24,6,2,1,1};
	int flag[9],i,sum=0,k=0;
	memset(flag,0,sizeof(flag));
	for(i=0;i<8;i++){
		flag[Square[i]]=1;
		k=0;
		while(Square[i]!=1){
			Square[i]--;
			if(flag[Square[i]]!=1)
				k++;
		}
		sum+=k*temp[i];
	}
	return sum;
}

void ReContor(int sum)
{
	int temp[8]={5040,720,120,24,6,2,1,1};
	int flag[9],k,Number;
	int i,j;
	memset(flag,0,sizeof(flag));
	for(i=0;i<8;i++){
 		k=sum/temp[i];
 		Number=k+1;
		for(j=1;j<=Number;j++)
			if(flag[j])
				Number++;
		flag[Number]=1;
		Square[i]=Number;
		sum%=temp[i];
	}
}

void Swap(int *a,int *b)
{
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
}

void Transform(int Choose)
{
	int i;
	switch(Choose){
		case 0:for(i=0;i<=3;i++){
					Swap(&Square[i],&Square[7-i]);
				}break;
		case 1:{
			Swap(&Square[0],&Square[3]);
			Swap(&Square[1],&Square[3]);
			Swap(&Square[2],&Square[3]);
			Swap(&Square[7],&Square[4]);
			Swap(&Square[6],&Square[4]);
			Swap(&Square[5],&Square[4]);		
			break;
		}
		case 2:{
			Swap(&Square[1],&Square[6]);
			Swap(&Square[2],&Square[6]);
			Swap(&Square[5],&Square[6]);
			break;
		}
	}
}

int Judge()
{
	int i;
	for(i=0;i<8;i++){
		if(Square[i]!=Goal[i])
			return 0;
	}
	return 1;
}

void Enqueue(int temp)
{
	Queue[rear]=temp;
	rear++;
}

int Dequeue()
{
	int temp;
	temp=Queue[front];
	front++;
	return temp;
}

void PrintPath(int temp,int n)
{
	if(temp==0)
		fprintf(fout,"%d\n",n);
	else{
		PrintPath(Path[temp][1],n+1);
		fprintf(fout,"%c",Path[temp][0]+'A'-1);
	}		
}
int main()
{
	int i,j,k,temp1,temp2;
	char c;
	fin=fopen("msquare.in","r");
	fout=fopen("msquare.out","w");
	memset(Path,0,sizeof(Path));
	memset(Dist,0,sizeof(Dist));
	
	i=0;
	for(i=0;i<8;i++)
		fscanf(fin,"%d",&Goal[i]);
	
	for(i=0;i<8;i++)
		Square[i]=i+1;
	
	temp1=Contor();
	Enqueue(temp1);
	Dist[temp1]=1;
	Path[temp1][0]=0;

	while(front!=rear){
		temp1=Dequeue();
		ReContor(temp1);
		if(Judge())
			break;
		for(i=0;i<3;i++){
			Transform(i);

			temp2=Contor();
			if(!Dist[temp2]){
				Enqueue(temp2);
				Dist[temp2]=1;
				Path[temp2][0]=i+1;
				Path[temp2][1]=temp1;
			}
			ReContor(temp1);
		}				
	}
	PrintPath(temp1,0);
	fprintf(fout,"\n");
	return 0;
}


不会做啊,学了下康托展开才自己写的,写的过程还是很纠结,说一下康托展开

X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0!

这样的话,可以把哈希数组压缩到8!大小

然后用BFS

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值