usaco Chapter 3 section 3.2 Magic Squares

/*
ID: niepeng1
LANG: C++
TASK:msquare
*/

/*

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.

此题的康托展开很巧妙,可以参考学习下

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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值