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:
8 7 6 5
1 2 3 4
B:
4 1 2 3
5 8 7 6
C:
1 7 2 4
8 6 3 5

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)

  1. 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)

  1. 7
  2. BCABCCB

——————————————————

就喜欢usaco题面一复制就下来了

8!很容易想到的是搜索,我们需要对一个长为8的全排列数列进行判重和记录

所以就是引入一个“中介数”

好吧,继续那个运用好几次的例子了……来源是一本余姚的书……

通过中介数(221)↑构造排列的过程如下:
  1. 首先这个排列的四个数的位置都是未知的:_ _ _ _
  2. 从左向右看中介数,第一个2表示4的右边有2个数比4小,则确定4的位置: _ 4 _ _
  3. 第二个2表示3的右边有2个比3小,则确定3的位置:3 4 _ _
  4. 第三个1表示2的右边有1个比2小,则确定2的位置:3 4 2 _
  5. 最后确定1的位置:3 4 2 1
同时可以知道,3421这个排列的序号是2*3!+2*2!+1*1!+0=17。
而且每个序号对应唯一的排列,我们知道一个排列求序号就可以了,所以就愉快的解决了
然后浅谈一下我的意识流:
对于一个全排列数列,也就是1234-4321,按字典序依次。
还是对于3421,3后面有两个数比它小,1在第一位时有3!个全排列,2在第一位时有3!个全排列,所以3421在这个数列的位置一定在2*3!之后在3*3!之前。
4后面有2个比它小,因为3这一位固定了,那么它之前会有3 1 打头的2!个排列,3 2 打头的2!个全排列,它在2*3!+2*2!个数之后
2后面有1个比它小,3 4 2 都固定了,那么也就是3 4 2 打头的1!个全排列即它本身。
所以映射了唯一的编号,我们就可以做题了。
 
这道题输出格式两点注意:
1是字母60个一行要换行
2是它给出的串是顺时针念的(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.)
例如题目中的:
1234
8765
如果给出一个描述就是12345678
所以样例中的描述是
2684
1375
当然这样并不舒服,所以我们把它的格式强行改成两排横着念就好了
  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[]={,,,,,,,,};
  29. int num(int x) {
  30. int ss[],l=;
  31. while(x>) {ss[++l]=x%;x/=;}
  32. int ret=;
  33. siji(i,,l) {
  34. int x=;
  35. xiaosiji(j,,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[],l=;
  44. while(x>) {ss[++l]=x%;x/=;}
  45. siji(i,,) swap(ss[i],ss[i+]);
  46. int ret=;
  47. gongzi(i,,) ret=ret*+ss[i];
  48. return ret;
  49. }
  50. int cha2(int x) {
  51. int ss[],l=;
  52. while(x>) {ss[++l]=x%;x/=;}
  53. int ss1[];
  54. siji(i,,) swap(ss[i],ss[-i+]);
  55. ss1[]=ss[];
  56. siji(i,,) ss1[i]=ss[i-];
  57. ss1[]=ss[];
  58. siji(i,,) ss1[i]=ss[i-];
  59. int ret=;
  60. siji(i,,) ret=ret*+ss1[i];
  61. return ret;
  62. }
  63. int cha3(int x) {
  64. int ss[],l=;
  65. while(x>) {ss[++l]=x%;x/=;}
  66. siji(i,,) swap(ss[i],ss[-i+]);
  67. int t=ss[];
  68. ss[]=ss[];ss[]=ss[];ss[]=ss[];ss[]=t;
  69. int ret=;
  70. siji(i,,) ret=ret*+ss[i];
  71. return ret;
  72. }
  73. int ys[];
  74. int mut[],step[],prev[],ans[],cnt;
  75. int to;
  76. char *str="$ABC";
  77. queue<int> q;
  78. void bfs() {
  79. q.push();
  80. mut[num()]=;
  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]==) {
  87. mut[w]=mut[num(now)]+;
  88. step[w]=;
  89. prev[w]=num(now);
  90. q.push(z);
  91. }
  92. z=cha2(now);
  93. w=num(z);
  94. if(mut[w]==) {
  95. mut[w]=mut[num(now)]+;
  96. step[w]=;
  97. prev[w]=num(now);
  98. q.push(z);
  99. }
  100. z=cha3(now);
  101. w=num(z);
  102. if(mut[w]==) {
  103. mut[w]=mut[num(now)]+;
  104. step[w]=;
  105. prev[w]=num(now);
  106. q.push(z);
  107. }
  108. }
  109.  
  110. }
  111. void solve() {
  112.  
  113. siji(i,,) {
  114. scanf("%d",&ys[i]);
  115. }
  116. siji(i,,) to=to*+ys[i];
  117. gongzi(i,,) to=to*+ys[i];
  118. bfs();
  119. printf("%d\n",mut[num(to)]-);
  120. int k=num(to);
  121. while(step[k]!=) {
  122. ans[++cnt]=step[k];
  123. k=prev[k];
  124. }
  125. gongzi(i,cnt,) {
  126. printf("%c",str[ans[i]]);
  127. if((cnt-i+)%==) 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吗……

虽然有点小开心呢但还是要说出题人别闹了这道题还是挺水的如果知道中介数的话……

USACO 3.2 Magic Squares的更多相关文章

  1. [hash-bfs]USACO 3.2 Magic Squares 魔板

    魔 板 魔板 魔板 题目描述 在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色 ...

  2. 洛谷 P2730 魔板 Magic Squares

    P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...

  3. [USACO3.2]魔板 Magic Squares

    松下问童子,言师采药去. 只在此山中,云深不知处.--贾岛 题目:魔板 Magic Squares 网址:https://www.luogu.com.cn/problem/P2730 这是一张有8个大 ...

  4. 「一本通 1.4 例 2」[USACO3.2]魔板 Magic Squares

    [USACO3.2]魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题 ...

  5. 840. Magic Squares In Grid (5月27日)

    开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...

  6. 洛谷 P2730 魔板 Magic Squares 解题报告

    P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...

  7. 哈希+Bfs【P2730】 魔板 Magic Squares

    没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...

  8. 3.2.5 Magic Squares 魔板

    3.2.5 Magic Squares 魔板 成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方 ...

  9. 【简●解】 LG P2730 【魔板 Magic Squares】

    LG P2730 [魔板 Magic Squares] [题目背景] 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 ...

随机推荐

  1. 链表c语言实现

    链表(c语言实现)--------------小练习   #include <stdio.h> #include <stdlib.h> #include <string. ...

  2. poj2488骑士之旅

    题目大意:国际象棋里面的马,有那么8种跳法,然后题目给出一个棋盘的大小p*q, 求有没有路线可以使得这个马能把整个棋盘的格全部走一遍,有的话按照字典序将第一条路线打印出来. 注意:国际象棋是行是数字, ...

  3. 迟到的 WPF 学习 —— 布局

    布局是 WPF 很重头的一部分内容,这一部分梳理和记录关于布局章节的知识点. 1. WPF 使用一种基于流(Flow-based)的概念来处理布局逻辑,将传统的基于"坐标"的思想尽 ...

  4. ios开发实践之UIDatePicker(已对之前无法解决的问题做了解答)

    需求:要做一个生日选择的控件,但除了选择之外还需要自定义几个控件,跟生日选择控件组合一起. 做法:自定义了一个UIImageView,并且作为背景.在这个背景view上再添加其他button和时间选择 ...

  5. jstl标签的fmt:formatDate格式化日期 String to Date

    使用fmt函数需在jsp中引入 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" ...

  6. 构建RESTful风格的WCF服务

    构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...

  7. NHibernate总结

    NHibernate总结 现在的项目中数据访问使用的是NHibernate的一个ORM框架,小弟也是在后期加入项目组,之前对NHibernate就一直没有接触过,所以一直在学习NHibernate,都 ...

  8. C# 操作 Excel 常见问题收集和整理

    C# 操作 Excel 常见问题收集和整理(定期更新,欢迎交流) 经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个 ...

  9. centos6.5 搭建nginx1.6.0 +gridfs +mongodb2.4..10环境

    一) 缘由 因为公司业务需要,需要搭建图片服务器,需求很简单:读取+上传图片,当时第一考虑用nginx来作,但考虑到单纯用nginx来作,无法水平扩展和管理,一旦遇到海量图片,就无办法 扩展.所以考虑 ...

  10. sharepoint 2013 自定义列表eventhandle权限控制

    记录一下如何在sharepoint server 2013自定义列表中,使用eventhandle控制自定义列表custom list的条目item权限. ///<summary> /// ...