[Luogu] 八数码难题

https://www.luogu.org/problemnew/show/P1379

long long ago

暴力bfs

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <queue>
#include <cstring>
#include <string>

using namespace std;
const string s_end = "123804765";

struct Node {
    string s;
    int step;
};
queue <Node> Q1;
map <string, bool> mp;
string s_start;
int Step;

inline void pd(string ss, int answer) {
    if(ss == s_end) {
        printf("%d",answer);
        exit(0);
    }
}

inline void bfs() {
    while(!Q1.empty()) {
        Node topp = Q1.front();
        Q1.pop();
        string s1 = topp.s;
        Step = topp.step;
        int f = s1.find('0');
        Node nxt;
        if(f == 0) {
            swap(s1[0], s1[1]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[0], s1[1]);
            swap(s1[0], s1[3]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 1) {
            swap(s1[0], s1[1]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[0], s1[1]);
            swap(s1[1], s1[2]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[1], s1[2]);
            swap(s1[1], s1[4]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 2) {
            swap(s1[1], s1[2]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[1], s1[2]);
            swap(s1[2], s1[5]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 3) {
            swap(s1[0], s1[3]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[0], s1[3]);
            swap(s1[3], s1[4]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[3], s1[4]);
            swap(s1[3], s1[6]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 4) {
            swap(s1[1], s1[4]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[1], s1[4]);
            swap(s1[3], s1[4]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[3], s1[4]);
            swap(s1[5], s1[4]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[5], s1[4]);
            swap(s1[7], s1[4]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 5) {
            swap(s1[5], s1[2]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[5], s1[2]);
            swap(s1[5], s1[4]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[5], s1[4]);
            swap(s1[5], s1[8]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 6) {
            swap(s1[6], s1[3]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[6], s1[3]);
            swap(s1[6], s1[7]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 7) {
            //4 7    6 7    8 7
            swap(s1[4], s1[7]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[4], s1[7]);
            swap(s1[6], s1[7]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[6], s1[7]);
            swap(s1[8], s1[7]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
        if(f == 8) {
            //58   78
            swap(s1[5], s1[8]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            swap(s1[5], s1[8]);
            swap(s1[7], s1[8]);
            pd(s1, topp.step + 1);
            if(!mp[s1]) {
                nxt.s = s1;
                nxt.step = topp.step + 1;
                Q1.push(nxt);
                mp[s1] = 1;
            }
            continue;
        }
    }
}

int main() {
    cin >> s_start;
    Node now;
    now.s = s_start;
    now.step = 0;
    Q1.push(now);
    bfs();
    return 0;
}
//283104765

 

转载于:https://www.cnblogs.com/shandongs1/p/8439131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值