BFS--魔板问题

题目来源:codeup 宽搜入门 问题D

题目描述
在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有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

解题心得

  1. 如果题目给的是矩阵,让你求出从一个矩阵状态变为另一个矩阵状态。我们一般会定义出一个结构体,结构体包含了对这个矩阵的描述(可以直接用二维矩阵,也可以将二维矩阵压缩为一维矩阵,还可以将矩阵进行哈希映射成一个数字,从而储存该数字) + 从开始状态到达该状态所用的步数(层数) + 到达该状态所经历的所有操作(可以用vector来存储)。
  2. 要判断两个矩阵的状态是否相等,如果每次都采用矩阵对应元素进行比较,那么会比较耗时,因此我们采用哈希映射后的值是否相等来判断两个矩阵的状态是否相等。
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
struct node
{
    int a[8];
    int step;
    vector<char> answer;
};
map<int, bool> M; //用map实现状态标记
queue<node> Q;
char op[3] = {'A', 'B', 'C'}; //三种操作

int hash_matrix(node &t) //将矩阵转换为一个整数
{
    int x = 0;
    for (int i = 0; i < 8; i++)
    {
        x = x * 10 + t.a[i];
    }
    return x;
}

bool stage_change(node &t, int n)
{
    if (n == 0)
    {
        for (int i = 0; i < 4; i++)
        {
            swap(t.a[i], t.a[i + 4]);
        }
        if (M[hash_matrix(t)] == false)
        {
            return true;
        }
    }
    else if (n == 1)
    {
        int temp1 = t.a[3];
        int temp2 = t.a[7];
        for (int i = 3; i >= 1; i--)
        {
            t.a[i] = t.a[i - 1];
            t.a[i + 4] = t.a[i + 3];
        }

        t.a[0] = temp1;
        t.a[4] = temp2;
        if (M[hash_matrix(t)] == false)
        {
            return true;
        }
    }
    else
    {
        int temp1 = t.a[1];
        t.a[1] = t.a[5];
        t.a[5] = t.a[6];
        t.a[6] = t.a[2];
        t.a[2] = temp1;
        if (M[hash_matrix(t)] == false)
        {
            return true;
        }
    }
    return false;
}

void BFS(node &end)
{
    int flag = 0;
    while (!Q.empty())
    {
        node top = Q.front();
        Q.pop();
        for (int i = 0; i < 3; i++)
        {
            node temp = top;           //新创建一个变量保存top,这样就可以不用还原操作了
            if (stage_change(temp, i)) //直接对temp进行更改状态
            {
                M[hash_matrix(temp)] = 1;
                temp.step = top.step + 1;
                temp.answer.push_back(op[i]);
                if (hash_matrix(temp) == hash_matrix(end))
                {
                    cout << temp.step << endl;
                    for (int i = 0; i < temp.answer.size(); i++)
                    {
                        cout << temp.answer[i];
                        if (i % 60 == 0 && i)
                        {
                            cout << endl;
                        }
                    }

                    flag = 1;
                    break;
                }
                Q.push(temp);
            }
        }
        if (flag)
        {
            break;
        }
    }
}
int main()
{
    node start;
    for (int i = 0; i < 4; i++)
    {
        start.a[i] = i + 1;
        start.a[i + 4] = 8 - i;
    }
    start.step = 0;
    M[hash_matrix(start)] = true;
    Q.push(start);
    node end;
    for (int i = 0; i < 4; i++)
    {
        cin >> end.a[i];
    }
    for (int i = 7; i >= 4; i--)
    {
        cin >> end.a[i];
    }
    if (hash_matrix(end) == hash_matrix(start)) //一定要注意判断不需要任何变换的情况,否则会有12%的错误率
    {
        cout << 0 << endl;
    }
    BFS(end);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39504764/article/details/89853932