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:
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.
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
思路:BFS+判重啊!今天才知道我以前迷迷糊糊听的hash是康托展开啊!
还是用了queue。。。
Executing...
Test 1: TEST OK [0.000 secs, 3404 KB]
Test 2: TEST OK [0.000 secs, 3404 KB]
Test 3: TEST OK [0.000 secs, 3404 KB]
Test 4: TEST OK [0.000 secs, 3404 KB]
Test 5: TEST OK [0.011 secs, 3404 KB]
Test 6: TEST OK [0.022 secs, 3404 KB]
Test 7: TEST OK [0.043 secs, 3404 KB]
Test 8: TEST OK [0.076 secs, 3404 KB]
All tests OK.
1 /* 2 ID:wuhuaju2 3 PROG:msquare 4 LANG:C++ 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstdlib> 9 #include <algorithm> 10 #include <cstring> 11 #include <string> 12 #include <queue> 13 using namespace std; 14 15 struct qq 16 { 17 int a[9]; 18 char s[220]; 19 } s,x; 20 queue<qq> q; 21 const int jie[]={1,1,2,6,24,120,720,5040,40320}; 22 int step,t1,t2,l,sum,cnt; 23 int b[10],tar[10]; 24 bool f[40330]; 25 void close() 26 { 27 fclose(stdin); 28 fclose(stdout); 29 exit(0); 30 } 31 32 void judge() 33 { 34 /* 35 printf("step:%d\n",step); 36 for (int i=1;i<=8;i++) 37 printf("%d ",x.a[i]); 38 cout<<'\n'; 39 puts(x.s); 40 */ 41 for (int i=1;i<=8;i++) 42 if (x.a[i]!=tar[i]) 43 return; 44 printf("%d\n",step); 45 puts(x.s); 46 close(); 47 } 48 49 bool hash() 50 { 51 sum=0;cnt=0; 52 for (int i=8;i>=1;i--) 53 { 54 cnt=0; 55 for (int j=i+1;j<=8;j++) 56 if (x.a[i]>x.a[j]) 57 cnt++; 58 sum+=cnt*jie[(x.a[i]-1)]; 59 } 60 /* 61 for (int i=1;i<=8;i++) 62 printf("%d",x.a[i]); 63 printf(" sum:%d\n",sum); 64 */ 65 if (f[sum]) 66 return true; 67 f[sum]=true; 68 return false; 69 } 70 71 void work() 72 { 73 x=s; 74 hash(); 75 judge(); 76 q.push(s); 77 step=0; 78 while (!q.empty()) 79 { 80 // printf("------------------------\n"); 81 step++; 82 l=q.size(); 83 for (int i=1;i<=l;i++) 84 { 85 s=q.front(); 86 x=s; 87 q.pop(); 88 for (int j=1;j<=4;j++) 89 b[j]=x.a[j]; 90 for (int j=1;j<=4;j++) 91 x.a[j]=x.a[j+4]; 92 for (int j=5;j<=8;j++) 93 x.a[j]=b[j-4]; //the 'A' 94 strcat(x.s,"A"); 95 judge(); 96 if (not hash()) 97 q.push(x); 98 x=s; 99 t1=x.a[4]; t2=x.a[8]; 100 x.a[4]=x.a[3];x.a[3]=x.a[2];x.a[2]=x.a[1]; 101 x.a[8]=x.a[7];x.a[7]=x.a[6];x.a[6]=x.a[5]; 102 x.a[1]=t1;x.a[5]=t2; //the 'B' 103 strcat(x.s,"B"); 104 judge(); 105 if (not hash()) 106 q.push(x); 107 x=s; 108 t1=x.a[7]; 109 x.a[7]=x.a[3];x.a[3]=x.a[2]; 110 x.a[2]=x.a[6];x.a[6]=t1;//the 'C' 111 strcat(x.s,"C"); 112 judge(); 113 if (not hash()) 114 q.push(x); 115 } 116 } 117 } 118 119 void init () 120 { 121 freopen("msquare.in","r",stdin); 122 freopen("msquare.out","w",stdout); 123 for (int i=1;i<=4;i++) 124 { 125 s.a[i]=i; 126 s.a[8-i+1]=i+4; 127 } 128 for (int i=1;i<=4;i++) 129 scanf("%d",&tar[i]); 130 for (int i=8;i>=5;i--) 131 scanf("%d",&tar[i]); 132 } 133 134 int main () 135 { 136 init(); 137 work(); 138 close(); 139 return 0; 140 }