问题 D: 【宽搜入门】魔板

【宽搜入门】魔板

时间限制: 1 Sec 内存限制: 128 MB

题目描述

在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板:
1 2 3 4
8 7 6 5
我们知道魔板的每一个方格都有一种颜色。这8种颜色用前8个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示。这是基本状态。
这里提供三种基本操作,分别用大写字母“A”,“B”,“C”来表示(可以通过这些操作改变魔板的状态):
“A”:交换上下两行;
“B”:将最右边的一列插入最左边;
“C”:魔板中央四格作顺时针旋转。
下面是对基本状态进行操作的示范:
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
对于每种可能的状态,这三种基本操作都可以使用。
你要编程计算用最少的基本操作完成基本状态到目标状态的转换,输出基本操作序列。

输入格式

输入有多组测试数据
只有一行,包括8个整数,用空格分开(这些整数在范围 1——8 之间),表示目标状态。

输出格式

Line 1: 包括一个整数,表示最短操作序列的长度。
Line 2: 在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出60个字符。

Sample Input

2 6 8 4 5 7 3 1

Sample Output

7
BCABCCB

这题按理来说不算难题却卡了我好久。查重很关键,不然会超时。本来想用map< int[2][4], bool> Hash 来标记每个情形,后来发现我想多了,数组应该是不能作为键的,所以只好把每种情形转换成一个int。这方法看起来挺笨的,可能有更好的办法吧。还有有多组数据需要注意一下。另外我忘记了处理原封不动即步数为0的情况,卡在错误12%好久。。。
C++代码如下。

#include<iostream>
#include<queue>
#include<vector>
using namespace std;

int matrix[2][4] = {{1, 2, 3, 4}, {8, 7, 6, 5}};
int final[2][4];
struct node{
    int M[2][4];  //进行当前操作后的魔板
    vector<char> bef;  //进行当前操作后的总操作步骤
} Node;
bool same(int a[2][4]){
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 4; j++){
            if(a[i][j] != final[i][j])
                return false;
        }
    }
    return true;
}
bool Hash[16500000] = {false};
int conv(int a[2][4]){ //八进制转十进制
    int b[8];
    for(int i = 0; i < 4; i++){
        b[i] = a[0][i] - 1;
    }
    for(int i = 4; i < 8; i++){
        b[i] = a[1][i - 4] - 1;
    }
    int num = 0;
    for(int i = 0; i < 8; i++){
        num = num * 8 + b[i];
    }
    return num;
}
void BFS(){
    queue<node> Q;
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 4; j++){
            Node.M[i][j] = matrix[i][j];
        }
    }
    if(same(Node.M)){   //如果在这不判断,原封不动的结果会是:操作AA,步数为2
        cout << '0' << endl;
        cout << endl;
        return ;    
    }
    Q.push(Node);
    while(!Q.empty()){
        node top = Q.front();
        Q.pop();
        //A
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 4; j++){
                Node.M[i][j] = top.M[1 - i][j];
            }
        }
        if(!Hash[conv(Node.M)]){
            Hash[conv(Node.M)] = true;
            Node.bef = top.bef;
            Node.bef.push_back('A');
            if(same(Node.M)){
                cout << Node.bef.size() << endl;
                for(int i =  0; i < Node.bef.size(); i++){
                    if(i > 0 && i % 60 == 0)
                        cout << endl;
                    cout << Node.bef[i];
                }
                Node.bef.clear();// 不清空会导致下一组数据是接在前一组后面的,下面BC同理
                cout << endl;
                return;
            }               
            Q.push(Node);
        }           
        //B
        for(int i = 0; i < 2; i++){
            for(int j = 1; j < 4; j++){
                Node.M[i][j] = top.M[i][j - 1];             
            }
        }
        Node.M[0][0] = top.M[0][3];
        Node.M[1][0] = top.M[1][3];     
        if(!Hash[conv(Node.M)]) {   
            Hash[conv(Node.M)] = true;
            Node.bef = top.bef; 
            Node.bef.push_back('B');
            if(same(Node.M)){
                cout << Node.bef.size() << endl;
                for(int i =  0; i < Node.bef.size(); i++){
                    if(i > 0 && i % 60 == 0)
                        cout << endl;
                    cout << Node.bef[i];
                }
                Node.bef.clear();
                cout << endl;
                return;
            }           
            Q.push(Node);
        }       
        //C
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 4; j++){
                Node.M[i][j] = top.M[i][j];             
            }
        }
        Node.M[0][1] = top.M[1][1];
        Node.M[0][2] = top.M[0][1];
        Node.M[1][1] = top.M[1][2];
        Node.M[1][2] = top.M[0][2]; 
        if(!Hash[conv(Node.M)]){
            Hash[conv(Node.M)] = true;
            Node.bef = top.bef;
            Node.bef.push_back('C');            
            if(same(Node.M)){
                cout << Node.bef.size() << endl;
                for(int i =  0; i < Node.bef.size(); i++){
                    if(i > 0 && i % 60 == 0)
                        cout << endl;
                    cout << Node.bef[i];
                }
                Node.bef.clear();
                cout << endl;
                return;
            }           
            Q.push(Node);
        }       
    }
}
int main(){
    while(cin >> final[0][0]){
        for(int i = 0; i < 16500000; i++){
            Hash[i] = false;
        }
        for(int i = 1; i < 4; i++){
            cin >> final[0][i];
        }
        for(int i = 0; i < 4; i++){
            cin >> final[1][3 - i];
        }
        BFS();
    }

    return 0;
}
/**************************************************************
    Problem: 5998
    User: 14041045
    Language: C++
    Result: 正确
    Time:104 ms
    Memory:18680 kb
****************************************************************/

猜你喜欢

转载自blog.csdn.net/ikechan/article/details/81708030