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
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#define name "msquare"
using namespace std;
map <string,int> num;
struct father
{
int f;//是由谁变来的
char c;//变的方式
}fa[1000000];
queue <string> Q;
string s,t;
char ABC[100000];
int ans,tot;
void print()
{
int x=0,y=tot+1;
while (y!=0)
{
ABC[++x]=fa[y].c;
y=fa[y].f;
}
cout<<x<<endl;
int tmp;
for (int i=x;i>=1;i--)
{
tmp++;
cout<<ABC[i];
if (tmp==60) cout<<endl;
}
cout<<endl;
}
void bfs()
{
int i,j;
while (!Q.empty())
{
string now=Q.front();
Q.pop();
string tmp="00000000";
for (i=0;i<=7;i++)
tmp[i]=now[7-i];
//cout<<tmp<<endl;
if (string(t)==string(tmp)) {fa[tot+1].f=num[now];fa[tot+1].c='A';print();break;}
if (num[tmp]==0)
{
Q.push(tmp);
++tot;
num[tmp]=tot;
fa[tot].f=num[now];
fa[tot].c='A';
}
tmp[0]=now[3];tmp[7]=now[4];
tmp[1]=now[0];tmp[2]=now[1];tmp[3]=now[2];
tmp[6]=now[7];tmp[5]=now[6];tmp[4]=now[5];
//cout<<tmp<<endl;
if (string(t)==string(tmp)) {fa[tot+1].f=num[now];fa[tot+1].c='B';print();break;}
if (num[tmp]==0)
{
Q.push(tmp);
++tot;
num[tmp]=tot;
fa[tot].f=num[now];
fa[tot].c='B';
}
tmp=now;
tmp[1]=now[6];tmp[2]=now[1];
tmp[5]=now[2];tmp[6]=now[5];
//cout<<tmp<<endl;
if (string(t)==string(tmp)) {fa[tot+1].f=num[now];fa[tot+1].c='C';print();break;}
if (num[tmp]==0)
{
Q.push(tmp);
++tot;
num[tmp]=tot;
fa[tot].f=num[now];
fa[tot].c='C';
}
}
}
int main()
{
freopen(name ".in","r",stdin);
freopen(name ".out","w",stdout);
int i,j;
t="00000000";
for (i=0;i<=7;i++)
cin>>t[i];
//cout<<t;
s="12345678";
if (t=="12345678") {cout<<"0";cout<<endl<<endl;return 0;}
num[s]=0;
Q.push(s);
bfs();
return 0;
}

深入探讨由Mr. Rubik发明的平面版魔方——魔方格(MagicSquares),以及如何通过三种基本变换将其从初始配置转换为目标配置。本文详细介绍了变换规则、输入输出格式,并提供了实现该任务的C++代码片段,旨在求解最小序列的基本变换。此教程特别适合热衷于算法挑战和编程技巧的读者。
509

被折叠的 条评论
为什么被折叠?



