HDU 1430(魔板)

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

struct Node //结点
{
    char operate; //操作
    int father; //该结点上一步结点的康托展开
} node[40500];

struct magic //魔板
{
    int s[10]; //字符串
    int id; //字符串对应的康托展开
};

int fac[10]; //阶乘
int st[10], ed[10]; //初始状态,终止状态
int pos[10]; //初始状态各数字的位置
char op[1000]; //操作过程

//初始化
void init()
{
    fac[0] = 1;
    for (int i = 1; i <= 8; i++)
        fac[i] = fac[i - 1] * i;
    for (int i = 0; i < 40500; i++)
        node[i].father = -1;
}

//康托展开
int cantor(int* s)
{
    int ans = 0;
    for (int i = 1; i <= 8; i++)
    {
        int k = 0;
        for (int j = i + 1; j <= 8; j++)
            if (s[i] > s[j])
                k++;
        ans += k * fac[8 - i];
    }
    return ans;
}

//A操作
void A(int* s)
{
    swap(s[1], s[5]);
    swap(s[2], s[6]);
    swap(s[3], s[7]);
    swap(s[4], s[8]);
}

//B操作
void B(int* s)
{
    swap(s[3], s[4]);
    swap(s[7], s[8]);
    swap(s[2], s[3]);
    swap(s[6], s[7]);
    swap(s[1], s[2]);
    swap(s[5], s[6]);
}

//C操作
void C(int* s)
{
    swap(s[2], s[3]);
    swap(s[2], s[7]);
    swap(s[2], s[6]);
}

//广搜,搜索从“12345678”到达其它状态的操作过程
void BFS()
{
    queue<magic> q;
    magic now, next;
    for (int i = 1; i <= 8; i++)
        now.s[i] = i;
    now.id = 0;
    q.push(now);
    while (!q.empty())
    {
        now = q.front();
        q.pop();
        //执行A操作
        next = now;
        A(next.s);
        next.id = cantor(next.s);
        if (node[next.id].father == -1)
        {
            node[next.id].father = now.id;
            node[next.id].operate = 'A';
            q.push(next);
        }
        //执行B操作
        next = now;
        B(next.s);
        next.id = cantor(next.s);
        if (node[next.id].father == -1)
        {
            node[next.id].father = now.id;
            node[next.id].operate = 'B';
            q.push(next);
        }
        //执行C操作
        next = now;
        C(next.s);
        next.id = cantor(next.s);
        if (node[next.id].father == -1)
        {
            node[next.id].father = now.id;
            node[next.id].operate = 'C';
            q.push(next);
        }
    }
}

int main()
{
    char pre[10], post[10];
    init();
    BFS();
    while (cin >> pre >> post)
    {
        for (int i = 0; i < strlen(pre); i++)
            st[i + 1] = pre[i] - '0';
        swap(st[5], st[8]);
        swap(st[6], st[7]);

        for (int i = 1; i <= 8; i++)
            pos[st[i]] = i;

        for (int i = 0; i < strlen(post); i++)
            ed[i + 1] = post[i] - '0';
        swap(ed[5], ed[8]);
        swap(ed[6], ed[7]);

        for (int i = 1; i <= 8; i++)
            ed[i] = pos[ed[i]];

        int id = cantor(ed);
        int cnt = 0;
        while (id)
        {
            op[++cnt] = node[id].operate;
            id = node[id].father;
        }
        for (int i = cnt; i >= 1; i--)
            cout << op[i];
        cout << endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值