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:

1234
8765

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:
8765
1234
B:
4123
5876
C:
1724
8635

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;
}

 



转载于:https://www.cnblogs.com/zhourongqing/archive/2012/09/13/2683965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值