Magic Squares(BFS + string)

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

 

      题意:

      有一个 2 X 4 的矩阵,排列方式如图所示,后给出 A,B,C 三种操作,问经过最少多少次的变换能得到所输入的序列,输出步数还有每一个步骤。

 

      思路:

      BFS + string。string 保存这个矩阵的情况,map 标记状态是否出现过。最后走一遍 BFS 即可。

 

      AC:

/*
ID:sum-g1
LANG:C++
PROG:msquare
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <iostream>
#include <map>

using namespace std;

typedef struct {
    string s, way;
    int ans;
} node;

string fin;
map<string, bool> m;

string A (string a) {
    for (int i = 0, j = 3; i < 4; ++i, --j) {
        char t;
        t = a[i];
        a[i] = a[4 + j];
        a[4 + j] = t;
    }
    return a;
}

string B (string a) {
    int ans = 0;
    char t = a[3], k = a[4];
    for (int i = 3, j = 0; i > 0; --i, ++j) {
        a[i] = a[i - 1];
        a[4 + j] = a[4 + j + 1];
    }
    a[0] = t;
    a[7] = k;
    return a;
}

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

void bfs() {
    node k;
    queue<node> q;
    k.s = "12345678";
    k.way = "";
    k.ans = 0;
    m[k.s] = 1;
    q.push(k);

    while (!q.empty()) {
        node x = q.front(); q.pop();

        if (fin == x.s) {
            cout << x.ans << endl;
            cout << x.way << endl;
            return;
        }

        node y;
        y.ans = x.ans + 1;
        y.s = A(x.s);
        if (!m[y.s]) {
            m[y.s] = 1;
            y.way = x.way + 'A';
            q.push(y);
        }

        y.s = B(x.s);
        if (!m[y.s]) {
            m[y.s] = 1;
            y.way = x.way + 'B';
            q.push(y);
        }

        y.s = C(x.s);
        if (!m[y.s]) {
            m[y.s] = 1;
            y.way = x.way + 'C';
            q.push(y);
        }
    }
}

int main() {

    freopen("msquare.in","r",stdin);
    freopen("msquare.out","w",stdout);

    for (int i = 1; i <= 8; ++i) {
        char c;
        scanf(" %c", &c);
        fin += c;
    }

    bfs();

    return 0;
}

 

 

你可能感兴趣的:(String)