USACO 3.2 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:
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
——————————————————
就喜欢usaco题面一复制就下来了
8!很容易想到的是搜索,我们需要对一个长为8的全排列数列进行判重和记录
所以就是引入一个“中介数”
好吧,继续那个运用好几次的例子了……来源是一本余姚的书……
-
首先这个排列的四个数的位置都是未知的:_ _ _ _
-
从左向右看中介数,第一个2表示4的右边有2个数比4小,则确定4的位置: _ 4 _ _
-
第二个2表示3的右边有2个比3小,则确定3的位置:3 4 _ _
-
第三个1表示2的右边有1个比2小,则确定2的位置:3 4 2 _
-
最后确定1的位置:3 4 2 1
1 /* 2 ID: ivorysi 3 PROG: msquare 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 #include <queue> 11 #include <set> 12 #include <vector> 13 #define siji(i,x,y) for(int i=(x);i<=(y);++i) 14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j) 15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i) 16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j) 17 #define inf 0x7fffffff 18 #define MAXN 400005 19 #define ivorysi 20 #define mo 97797977 21 #define ha 974711 22 #define ba 47 23 #define fi first 24 #define se second 25 //#define pis pair<int,string> 26 using namespace std; 27 typedef long long ll; 28 int fac[]={0,0,1,2,6,24,120,720,5040}; 29 int num(int x) { 30 int ss[15],l=0; 31 while(x>0) {ss[++l]=x%10;x/=10;} 32 int ret=0; 33 siji(i,2,l) { 34 int x=0; 35 xiaosiji(j,1,i) { 36 if(ss[j]<ss[i]) ++x; 37 } 38 ret+=x*fac[i]; 39 } 40 return ret; 41 } 42 int cha1(int x) { 43 int ss[15],l=0; 44 while(x>0) {ss[++l]=x%10;x/=10;} 45 siji(i,1,4) swap(ss[i],ss[i+4]); 46 int ret=0; 47 gongzi(i,8,1) ret=ret*10+ss[i]; 48 return ret; 49 } 50 int cha2(int x) { 51 int ss[15],l=0; 52 while(x>0) {ss[++l]=x%10;x/=10;} 53 int ss1[15]; 54 siji(i,1,4) swap(ss[i],ss[8-i+1]); 55 ss1[1]=ss[4]; 56 siji(i,2,4) ss1[i]=ss[i-1]; 57 ss1[5]=ss[8]; 58 siji(i,6,8) ss1[i]=ss[i-1]; 59 int ret=0; 60 siji(i,1,8) ret=ret*10+ss1[i]; 61 return ret; 62 } 63 int cha3(int x) { 64 int ss[15],l=0; 65 while(x>0) {ss[++l]=x%10;x/=10;} 66 siji(i,1,4) swap(ss[i],ss[8-i+1]); 67 int t=ss[2]; 68 ss[2]=ss[6];ss[6]=ss[7];ss[7]=ss[3];ss[3]=t; 69 int ret=0; 70 siji(i,1,8) ret=ret*10+ss[i]; 71 return ret; 72 } 73 int ys[10]; 74 int mut[50005],step[50005],prev[50005],ans[50005],cnt; 75 int to; 76 char *str="$ABC"; 77 queue<int> q; 78 void bfs() { 79 q.push(12348765); 80 mut[num(12348765)]=1; 81 while(!q.empty()) { 82 int now=q.front();q.pop(); 83 if(now==to) break; 84 int z=cha1(now); 85 int w=num(z); 86 if(mut[w]==0) { 87 mut[w]=mut[num(now)]+1; 88 step[w]=1; 89 prev[w]=num(now); 90 q.push(z); 91 } 92 z=cha2(now); 93 w=num(z); 94 if(mut[w]==0) { 95 mut[w]=mut[num(now)]+1; 96 step[w]=2; 97 prev[w]=num(now); 98 q.push(z); 99 } 100 z=cha3(now); 101 w=num(z); 102 if(mut[w]==0) { 103 mut[w]=mut[num(now)]+1; 104 step[w]=3; 105 prev[w]=num(now); 106 q.push(z); 107 } 108 } 109 110 } 111 void solve() { 112 113 siji(i,1,8) { 114 scanf("%d",&ys[i]); 115 } 116 siji(i,1,4) to=to*10+ys[i]; 117 gongzi(i,8,5) to=to*10+ys[i]; 118 bfs(); 119 printf("%d\n",mut[num(to)]-1); 120 int k=num(to); 121 while(step[k]!=0) { 122 ans[++cnt]=step[k]; 123 k=prev[k]; 124 } 125 gongzi(i,cnt,1) { 126 printf("%c",str[ans[i]]); 127 if((cnt-i+1)%60==0) puts(""); 128 } 129 puts(""); 130 } 131 int main(int argc, char const *argv[]) 132 { 133 #ifdef ivorysi 134 freopen("msquare.in","r",stdin); 135 freopen("msquare.out","w",stdout); 136 #else 137 freopen("f1.in","r",stdin); 138 #endif 139 solve(); 140 }
哦还想起来一个老师让我刷但是我特别不想刷的一个界面特别不友好特别有那种盗版的感觉但是老师说它好的oj上有这道题。
一段很长的空白之后……
出题人是没有刷过USACO吗……
虽然有点小开心呢但还是要说出题人别闹了这道题还是挺水的如果知道中介数的话……
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程
· SQL Server 内存占用高分析及解决办法(超详细)
· 20250116 支付宝出现重大事故 有感
· ffmpeg简易播放器(1)--了解视频格式