USACO Section 3.2 PROB Magic Squares

题目:(摘自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:

1234
8765

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

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)

7
BCABCCB


分析:

因为只有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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值