[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

 

posted @ 2018-02-10 16:42  xayata  阅读(150)  评论(0编辑  收藏  举报