zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    javaBean为什么要实现Serializable接口?
    OpenLayers3的WMS空间查询实现多个图层
    (WPS) 网络地理信息处理服务
    window.open跳过浏览器拦截
    linux改权限
    element-ui MessageBox的bug
    element-ui上传文件带token
    MySQL启动出现The server quit without updating PID file错误解决办法
    重装应用商店
    vscode在vue-cli中按照ESlint自动格式化代码
  • 原文地址:https://www.cnblogs.com/shandongs1/p/8439131.html
Copyright © 2011-2022 走看看