USACO 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:
| 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
/*
ID: ivorysi
PROG: msquare
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
//#define pis pair<int,string>
using namespace std;
typedef long long ll;
int fac[]={,,,,,,,,};
int num(int x) {
int ss[],l=;
while(x>) {ss[++l]=x%;x/=;}
int ret=;
siji(i,,l) {
int x=;
xiaosiji(j,,i) {
if(ss[j]<ss[i]) ++x;
}
ret+=x*fac[i];
}
return ret;
}
int cha1(int x) {
int ss[],l=;
while(x>) {ss[++l]=x%;x/=;}
siji(i,,) swap(ss[i],ss[i+]);
int ret=;
gongzi(i,,) ret=ret*+ss[i];
return ret;
}
int cha2(int x) {
int ss[],l=;
while(x>) {ss[++l]=x%;x/=;}
int ss1[];
siji(i,,) swap(ss[i],ss[-i+]);
ss1[]=ss[];
siji(i,,) ss1[i]=ss[i-];
ss1[]=ss[];
siji(i,,) ss1[i]=ss[i-];
int ret=;
siji(i,,) ret=ret*+ss1[i];
return ret;
}
int cha3(int x) {
int ss[],l=;
while(x>) {ss[++l]=x%;x/=;}
siji(i,,) swap(ss[i],ss[-i+]);
int t=ss[];
ss[]=ss[];ss[]=ss[];ss[]=ss[];ss[]=t;
int ret=;
siji(i,,) ret=ret*+ss[i];
return ret;
}
int ys[];
int mut[],step[],prev[],ans[],cnt;
int to;
char *str="$ABC";
queue<int> q;
void bfs() {
q.push();
mut[num()]=;
while(!q.empty()) {
int now=q.front();q.pop();
if(now==to) break;
int z=cha1(now);
int w=num(z);
if(mut[w]==) {
mut[w]=mut[num(now)]+;
step[w]=;
prev[w]=num(now);
q.push(z);
}
z=cha2(now);
w=num(z);
if(mut[w]==) {
mut[w]=mut[num(now)]+;
step[w]=;
prev[w]=num(now);
q.push(z);
}
z=cha3(now);
w=num(z);
if(mut[w]==) {
mut[w]=mut[num(now)]+;
step[w]=;
prev[w]=num(now);
q.push(z);
}
} }
void solve() { siji(i,,) {
scanf("%d",&ys[i]);
}
siji(i,,) to=to*+ys[i];
gongzi(i,,) to=to*+ys[i];
bfs();
printf("%d\n",mut[num(to)]-);
int k=num(to);
while(step[k]!=) {
ans[++cnt]=step[k];
k=prev[k];
}
gongzi(i,cnt,) {
printf("%c",str[ans[i]]);
if((cnt-i+)%==) puts("");
}
puts("");
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("msquare.in","r",stdin);
freopen("msquare.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}
哦还想起来一个老师让我刷但是我特别不想刷的一个界面特别不友好特别有那种盗版的感觉但是老师说它好的oj上有这道题。


一段很长的空白之后……

出题人是没有刷过USACO吗……
虽然有点小开心呢但还是要说出题人别闹了这道题还是挺水的如果知道中介数的话……
USACO 3.2 Magic Squares的更多相关文章
- [hash-bfs]USACO 3.2 Magic Squares 魔板
魔 板 魔板 魔板 题目描述 在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色 ...
- 洛谷 P2730 魔板 Magic Squares
P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...
- [USACO3.2]魔板 Magic Squares
松下问童子,言师采药去. 只在此山中,云深不知处.--贾岛 题目:魔板 Magic Squares 网址:https://www.luogu.com.cn/problem/P2730 这是一张有8个大 ...
- 「一本通 1.4 例 2」[USACO3.2]魔板 Magic Squares
[USACO3.2]魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题 ...
- 840. Magic Squares In Grid (5月27日)
开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...
- 洛谷 P2730 魔板 Magic Squares 解题报告
P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...
- 哈希+Bfs【P2730】 魔板 Magic Squares
没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...
- 3.2.5 Magic Squares 魔板
3.2.5 Magic Squares 魔板 成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方 ...
- 【简●解】 LG P2730 【魔板 Magic Squares】
LG P2730 [魔板 Magic Squares] [题目背景] 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 ...
随机推荐
- 生活沉思录 via 哲理小故事(四)
1.围墙里的墓碑 第一次世界大战期间,驻守意大利某小镇的年轻军官结识了镇上的牧师.虽然军官信仰信教,而牧师是天主教牧师,但两人一见如故. 军官在一次执行任务中身负重伤,弥留之际嘱托牧师无论如何要把自己 ...
- go语言 strconv.ParseInt 的例子
golang strconv.ParseInt 是将字符串转换为数字的函数,功能灰常之强大,看的我口水直流. func ParseInt(s string, base int, bitSize int ...
- WebForm和Asp.Net MVC的理解
我对WebForm和Asp.Net MVC的理解 比较WebForm和Mvc的请求处理方式 首先简单了解一下Asp.Net中怎么对页面进行请求处理的: 在管道的第7-8个事件之间,有一个MapHt ...
- C#线程池用法
C#线程池用法 在C#编程语言中,使用线程池可以并行地处理工作,当强制线程和更新进度条时,会使用内建架构的ThreadPool类,为批处理使用多核结构,这里我们来看在C#编程语言中一些关于来自Syst ...
- [转]Disabling ASLR on individual iOS applications when using iOS 6.0.1
ASLR: Address Space Layout Randomization 查看应用是否进行了 ASLR 保护的方法:otool -hv ${File-Path} I recently enco ...
- 企业架构研究总结(38)——TOGAF架构能力框架之架构能力建设和架构治理
为了确保架构功能在企业中能够被成功地运用,企业需要通过建立适当的组织结构.流程.角色.责任和技能来实现其自身的企业架构能力,而这也正是TOGAF的架构能力框架(Architecture Capabil ...
- 关于UITextfield弹出键盘解决方案
解决的问题:当你点击一个UITextfield时,不想让其弹出键盘,如果你觉得不就是取消其第一响应者嘛,resignRespond一下不就行了嘛,确实,如果你只是在其编辑完成后让其键盘消失,那这个就够 ...
- 用django搭建一个简易blog系统(翻译)(三)
06. Connecting the Django admin to the blog app Django 本身就带有一个应用叫作Admin,而且它是一个很好的工具 在这一部分,我们将要激活admi ...
- 微信移动支付V3开发详细教程服务端采用.net mvc webapi(C#)
转自:http://www.kwstu.com/ArticleView/netmvc_201511132050268716 最近开发手机app需要实现移动支付功能,由于考虑支付安全将微信支付生成签名写 ...
- hdu2837数论
http://acm.hdu.edu.cn/showproblem.php?pid=2837 // a^b%p=a^(b%phi(p)+phi(p))%p #include<iostream&g ...