USACO Section 3.2 PROB Magic Squares

来源:互联网 发布:手机数据连接不到网络 编辑:程序博客网 时间:2024/04/28 12:15

题目:(摘自USACO官网)


Following the success of the magic cube, Mr. Rubik invented its planarversion, called magic squares. This is a sheet composed of 8 equal-sizedsquares:

12348765

In this task we consider the version where each square has a differentcolor. Colors are denoted by the first 8 positive integers. A sheetconfiguration is given by the sequence of colors obtained by readingthe colors of the squares starting at the upper left corner and goingin clockwise direction. For instance, the configuration of Figure3 is given by the sequence (1,2,3,4,5,6,7,8). This configurationis 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 tothe initial squares given above:

A:87651234B:41235876C:17248635

All possible configurations are available using the three basictransformations.

You are to write a program that computes a minimal sequence of basictransformations that transforms the initial configuration above to aspecific 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 lengthof the shortest transformation sequence.Line 2:The lexically earliest string of transformations expressedas a string of characters, 60 per line except possibly the last line.

SAMPLE OUTPUT (file msquare.out)

7BCABCCB


分析:

因为只有8!种状态,故穷举即可。实际上状态间形成一个有向图,图中节点表示各个square,边表示变换。

由于是求最少次数的变换,故使用BFS搜索。

判重采用Hashtable最好了,但C++标准库中暂时没有,可以使用set。


代码:C++

/*
ID: randyji1
PROG: msquare
LANG: C++
*/


#include <fstream>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cassert>
using namespace std;

ifstream fin("msquare.in");

ofstream fout("msquare.out");


struct Node {
    unsigned int sq;
    string trans;
    Node() {}
    Node(string t, unsigned int n) {trans = t; sq = n;}
};

unsigned int initState = 12345678;

unsigned int dest;
void getInput() {
    dest = 0;
    int tmp;
    for(int i = 0; i < 8; i++) {
        fin >> tmp;
        dest = dest * 10 + tmp;
    }
}

unsigned int transA(unsigned int orig) {
    unsigned int rs = 0;
    for(int i = 0; i < 8; i++) {
        rs = rs * 10 + orig % 10;
        orig /= 10;
    }
    return rs;
    
}

unsigned int transB(unsigned int orig) {
    unsigned int rs = 0;
    unsigned int upper4 = orig / 10000;
    unsigned int lower4 = orig % 10000;
    upper4 = upper4%10*1000+upper4/10;
    lower4 = lower4/1000 + lower4%1000*10;
    return upper4 * 10000 + lower4;
}

unsigned int transC(unsigned int orig) {
    unsigned int rs = 0;
    unsigned int digits[8];
    digits[0] = orig / 10000000;
    digits[3] = orig / 10000 % 10;
    digits[4] = orig / 1000 % 10;
    digits[7] = orig % 10;
    digits[1] = orig / 10 % 10;
    digits[2] = orig / 1000000 % 10;
    digits[5] = orig / 100000 % 10;
    digits[6] = orig / 100 % 10;
    for(int i = 0; i < 8; i++)
        rs = rs * 10 + digits[i];
    return rs;
}

void printResult(string trans) {
    
    fout << trans.size() << endl;
    for(int i = 0; i < trans.size(); i++) {
        fout << trans[i];
        if((i + 1) % 60 == 0) fout << endl;
    }
    if((trans.size() % 60) != 0) fout << endl;
    if(trans.size() == 0) fout << endl;
}

void process(unsigned int init, unsigned int des) {
    set<unsigned int> sqSet;
    queue<Node> st;
    st.push(Node("", init));
    sqSet.insert(init);

    while(!st.empty()) {
        Node node = st.front();
        st.pop();

        unsigned int current = node.sq;
        //cout << current << endl;
        if(current == des) {
            printResult(node.trans);
            return;
        }
        
        unsigned int t;
        t = transA(current);
        if(sqSet.count(t) == 0) {
            st.push(Node(node.trans + 'A', t));
            sqSet.insert(t);
        }

        t = transB(current);
        if(sqSet.count(t) == 0) {
            st.push(Node(node.trans + 'B', t));
            sqSet.insert(t);
        }

        t = transC(current);
        if(sqSet.count(t) == 0) {
            st.push(Node(node.trans + 'C', t));
            sqSet.insert(t);
        }

    }
    //assert(false);
}



int main() {
   assert(fin && fout);
   getInput();
   process(initState, dest);
   return 0;
}


原创粉丝点击