Sicily 1515. 魔版C

来源:互联网 发布:淘宝卖家强制取消订单 编辑:程序博客网 时间:2024/06/12 12:05

1515. 魔版C

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

Description

魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示。
其初始状态是
1 2 3 4
5 6 7 8
对魔板可进行三种基本操作:
A操作(左右两列互换):
3 4 1 2
7 8 5 6
B操作(每次以行循环左移一个):
2 3 4 1
6 7 8 5
C操作(中间四小块逆时针转一格):
1 3 7 4
5 2 6 8
用上述三种基本操作,可将任一种状态转换成另一种状态。

Input

输入包括多个要求解的魔板,每个魔板用三行描述。
第一行步数N(N<=100),表示最多容许的步数。
第二、第三行表示目标状态,按照魔板的形状,颜色用1到8的表示。
当N等于-1的时候,表示输入结束。

Output

对于每一个要求解的魔板,输出一行。
首先是一个整数M,表示你找到解答所需要的步数。接着若干个空格之后,从第一步开始按顺序给出M步操作(每一步是A、B或C),相邻两个操作之间没有任何空格。
注意:如果不能达到,则M输出-1即可。

Sample Input

44 1 2 38 5 6 721 2 3 45 6 7 801 3 7 45 2 6 8-1

Sample Output

2 AB0-1

Problem Source

2007???????????

// Problem#: 1515// Submission#: 3316797// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <algorithm>#include <iostream>#include <string>#include <stdio.h>#include <queue>#include <string.h>#include <vector>#include <iomanip>#include <map>#include <stack>#include <functional>#include <list>#include <cmath>using namespace std;struct step {    string way;    string num;    step() {}    step(string w, string n) {        way = w;        num = n;    }};string S[40325];int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};bool exist[40325];inline int KT(string s) {    int i, j, t, sum;    sum = 0;    for (int i = 0; i < s.size(); i++) {        t = 0;        for (int j = i + 1; j < s.size(); j++) {            if (s[j] < s[i]) t++;        }        sum += t * fac[s.size() - i - 1];    }    return sum;}void BFS() {    queue<step> q;    q.push(step("", "12345678"));    int pos = KT("12345678");    exist[pos] = true;    S[pos] = "";    while (!q.empty()) {        step temp = q.front();        q.pop();        string tempa, tempb, tempc;        tempa = tempb = tempc = temp.num;        // a reverse        swap(tempa[0], tempa[2]);        swap(tempa[1], tempa[3]);        swap(tempa[4], tempa[6]);        swap(tempa[5], tempa[7]);        // b reverse        char tempChar = tempb[0];        tempb[0] = tempb[1];        tempb[1] = tempb[2];        tempb[2] = tempb[3];        tempb[3] = tempChar;        tempChar = tempb[4];        tempb[4] = tempb[5];        tempb[5] = tempb[6];        tempb[6] = tempb[7];        tempb[7] = tempChar;        //c reverse        tempChar = tempc[1];        tempc[1] = tempc[2];        tempc[2] = tempc[6];        tempc[6] = tempc[5];        tempc[5] = tempChar;        int pos = KT(tempa);        if (!exist[pos]) {            exist[pos] = true;            S[pos] = temp.way + "A";            q.push(step(temp.way + "A", tempa));        }        pos = KT(tempb);        if (!exist[pos]) {            exist[pos] = true;            S[pos] = temp.way + "B";            q.push(step(temp.way + "B", tempb));        }        pos = KT(tempc);        if (!exist[pos]) {            exist[pos] = true;            S[pos] = temp.way + "C";            q.push(step(temp.way + "C", tempc));        }    }}int main() {    BFS();    std::ios::sync_with_stdio(false);    while (1) {        int n;        cin >> n;        if (n == -1) break;        string s;        s.resize(8);        for (int i = 0; i < 8; i++) {            cin >> s[i];        }        int pos = KT(s);        if (S[pos].size() > n) cout << -1 << endl;        else {            cout << S[pos].size() << "  " << S[pos] << endl;        }    }    //getchar();    //getchar();        return 0;}                                 


0 0