USACO Section 3.2 Magic Squares(BFS)

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)

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
题意:一块模板有三种操作:分别是A、B、C,A操作时交换上下两行,B操作时将最后一列插在最前面,C操作时将中间四个数顺时针旋转一下。告诉你初始状态和目标状态,求最少多少次操作可以变化到目标状态,并且输出其操作序列。输入的目标状态时按顺时针输入的。
分析:BFS
View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: msquare
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<map>
#define MAXN 10000

using namespace std;

struct state{string s,ans;int step;};
char ans[MAXN];
map<string,bool>visited;
string end;

string opA(string a)
{
    int i;
    for(i=0;i<4;i++) swap(a[i],a[i+4]);
    return a;
}

string opB(string a)
{
    char t;
    t=a[3];a[3]=a[2];a[2]=a[1];a[1]=a[0];a[0]=t;
    t=a[7];a[7]=a[6];a[6]=a[5];a[5]=a[4];a[4]=t;
    return a;
}

string opC(string a)
{
    char t;
    t=a[1];a[1]=a[5];a[5]=a[6];a[6]=a[2];a[2]=t;
    return a;
}

void BFS()
{
    bool flag;
    state t,tt;
    t.step=0;t.s="12348765";t.ans="";
    queue<state>Q;
    Q.push(t);
    visited[t.s]=true;
    while(!Q.empty())
    {
        t=Q.front();Q.pop();
        if(t.s==end)
        {
            cout<<t.step<<endl<<t.ans<<endl;
            return ;
        }
        tt.ans=t.ans;
        tt.step=t.step+1;
        flag=false;

        tt.s=opA(t.s);
        if(!visited[tt.s])
        {
            visited[tt.s]=true;
            tt.ans+='A';
            Q.push(tt);
            flag=true;
        }

        tt.s=opB(t.s);
        if(!visited[tt.s])
        {
            visited[tt.s]=true;
            if(!flag) tt.ans+='B';
            else tt.ans[t.step]='B';
            Q.push(tt);
            flag=true;
        }

        tt.s=opC(t.s);
        if(!visited[tt.s])
        {
            visited[tt.s]=true;
            if(!flag) tt.ans+='C';
            else tt.ans[t.step]='C';
            Q.push(tt);
        }
    }
}

int main()
{
    freopen("msquare.in","r",stdin);
    freopen("msquare.out","w",stdout);
    int i;
    string t;
    for(i=1;i<=8;i++)
    {
        cin>>t;end+=t;
    }
    swap(end[4],end[7]);
    swap(end[5],end[6]);
    BFS();
    return 0;
}

 



posted @ 2012-09-13 20:49  mtry  阅读(687)  评论(0编辑  收藏  举报