zoukankan      html  css  js  c++  java
  • AOJ 0121 Seven Puzzle {广度优先搜索}(*)

    原题

    这里写图片描写叙述

    这里写图片描写叙述

    这里写图片描写叙述

    题意

    题意是有一个输入,比方:

    1 0 2 3 4 5 6 7

    摆成例如以下形状:

    1 0 2 3
    4 5 6 7

    0表示空格。其它数字能够移动到0的位置。最后须要到例如以下形状:

    0 1 2 3
    4 5 6 7

    上面的这样的情况是须要移动一步,也就是0和1直接移动就好。

    代码

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<map>
    
    using namespace std;
    
    int dx[4] = { 1,-1,4,-4 };
    map<string, int>res;
    
    void solve(void) {
        queue<string>que;
        que.push("01234567");
        while (!que.empty()) {
            string v = que.front();
            que.pop();
            int pos = 0;
            for (int i = 0; i < 8; i++)
                if (v[i] == '0')pos = i;
    
            for (int i = 0; i < 4; i++) {
                if (0 <= pos + dx[i] && pos + dx[i] < 8 &&
                    !(pos == 3 && i == 0) && !(pos == 4 && i == 1)) {
                    string u = v;
                    swap(u[pos], u[pos + dx[i]]);
    
                    if (res[u] == 0) {
                        que.push(u);
                        res[u] = res[v] + 1;
                    }
                }
            }
        }
    }
    
    int main(void) {
        int in;
        res["01234567"] = 1;
        solve();
        while (true) {
            string s;
            for (int i = 0; i < 8; i++) {
                if (!(cin >> in))return 0;
                s += in + '0';
            }
            cout << res[s] - 1 << endl;
        }
        return 0;
    }
  • 相关阅读:
    20170602
    使用布局规划页面
    商品的删除
    修改页面的 修改图片
    商品修改
    引入行高亮显示
    添加一个时间插件: 1.把插件放到 public目录 下 datetimepicker
    排序,搜索 代码
    搜索
    翻页代码,商品列表页;
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7094422.html
Copyright © 2011-2022 走看看