八数码问题

八数码问题

题目描述

这里写图片描述

输入

两个3×3的矩阵,0表示空格,第一个表示初始状态,第二个表示目标状态

输出

最少步数,如果无解就输出-1

样例输入

2 8 3
1 6 4
7 0 5
1 2 3
8 0 4
7 6 5

样例输出

5

题目解读

这应该是广搜最经典的一道例题了。
这里拓展一个点:康拓展开
康拓展开网上写的真是太麻烦了
其实说白了就是,求一个1~n的序列按照字典序在全部从小到大排好的1~n全排列中的第几个
example:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
等。。
所以,Cantor(1 2 3 4) = 1;
Cantor(1 2 4 3) = 2;
等。。

大概就是这个样子滴(具体的公式可以砍下面的代码或者其问问度娘)
这个有什么用呢?
我们都知道,bfs搜的时候是要标记是否够访问过的(vis[])
但是这道题中是个数组,很难表示状态,所以我们可以用康拓展开的值用来标记访问
其他的都很是一样。。

code

#include <bits/stdc++.h>

using namespace std;

inline int read() {
    int x = 0 , f = 1; char ch = getchar();
    for ( ; !isdigit(ch) ; ch = getchar()) if (ch == '-') f = -1;
    for ( ; isdigit(ch) ; ch = getchar()) x = x * 10 + ch - '0';
    return x * f;
}

const int fac[]={1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880};
const int dirx[4] = {0 , 0 , 1 , -1};
const int diry[4] = {1 , -1 , 0 , 0};
const int maxn = 4e6 + 1;

struct Node {
    int con[10];
    int zero , step , hashVal;
}now , _end;

int step;

bool vis[maxn];

queue <Node> Q;

int Cantor(int s[]) {
    int sum = 0;
    for (int i = 0 ; i < 9 ; i ++) {
        int cnt = 0;
        for (int j = i + 1 ; j < 9 ; j ++) {
            if (s[i] > s[j]) {
                cnt ++;
            }
        }

        sum += cnt * fac[8 - i];
    }

    return sum + 1;
}

bool bfs() {
    Q.push(now);
    vis[now.hashVal] = true;

    while (Q.size()) {
        Node fr = Q.front(); Q.pop();
        /*
        int cnt = 0;
        for (int i = 1 ; i <= 3 ; i ++) {
            for (int j = 1 ; j <= 3 ; j ++) {
                cout << fr.con[cnt ++] << " ";
            }cout << endl;
        }cout << endl;
        */
        int zx = fr.zero / 3 , zy = fr.zero % 3;

        if (fr.hashVal == _end.hashVal) {
            step = fr.step;
            return true;
        }

        for (int i = 0 ; i < 4 ; i ++) {
            int nextx = zx + dirx[i] , nexty = zy + diry[i];

            if (nextx < 0 || nexty < 0 || nextx > 2 || nexty > 2) {
                continue;
            }

            Node next = fr;
            next.zero = 3 * nextx + nexty;
            next.con[fr.zero] = next.con[next.zero];
            next.con[next.zero] = 0;
            next.hashVal = Cantor(next.con);

            if (vis[next.hashVal]) {
                continue;
            }
            else {
                vis[next.hashVal] = true;
                next.step ++;
                Q.push(next);
            }
        }
    }
}

int main() {
    for (int i = 0 ; i < 9 ; i ++) {
        int x = read();
        if (!x) {
            now.zero = i;
        }

        now.con[i] = x;
    }

    for (int i = 0 ; i < 9 ; i ++) {
        int x = read();
        if (!x) {
            _end.zero = i;
        }
        _end.con[i] = x;
    }

    now.hashVal = Cantor(now.con);
    _end.hashVal = Cantor(_end.con);

    bool ans = bfs();
    if (ans) {
        printf("%d\n" , step);
    }
    else {
        puts("-1");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值