码迷,mamicode.com
首页 > Windows程序 > 详细

AcWing 845. 八数码

时间:2021-02-18 13:20:58      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:手动   iostream   表示   namespace   看到了   turn   时间   cpp   八数码   

AcWing 845. 八数码


解题思路

这道题,放在了BFS的分类下面

第一反应是从一个状态开始广搜

不过状态的表示,存储和转移,以及最短距离dist[]数组,都没有很好的想法

最终思路

看完y总的课,看到了他用一个字符串来表示一个3x3的矩阵,还真是个好想法

状态表示就用一个字符串

用一个queue<string> q;作为BFS用的队列,然后用一个unordered_map<string, int> d;作为距离数组

这里用unordered_map是因为它出色的时间复杂度,既然这里不需要用迭代器从头到尾走一遍,只需要读d[t],那最好的选择还是基于Hash,\(O(1)\)复杂度的unordered_map了

(其实可以手动hash字符串,可惜我太懒x,STL大法好)

最终源码

#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <queue>

using namespace std;

int bfs(string state){
    queue<string> q;
    unordered_map<string, int> d;
    
    q.push(state);
    d[state] = 0;
    
    int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    
    string ed = "12345678x";
    
    while(q.size()){
        auto t = q.front();
        q.pop();
        
        if(t == ed) return d[t];
        
        int dist = d[t];
        int k = t.find(‘x‘);
        int x = k / 3, y = k % 3;
        for(int i = 0; i < 4; i ++ ){
            int a = x + dx[i], b = y + dy[i];
            if(a >= 0 && a < 3 && b >= 0 && b < 3){
                swap(t[a * 3 + b], t[k]);
                if(!d.count(t)){
                    d[t] = dist + 1;
                    q.push(t);
                }
                swap(t[a * 3 + b], t[k]);
            }
        }
    }
    
    return -1;
}

int main(){
    string state;
    
    for(int i = 0; i < 9; i ++ ){
        char c;
        cin >> c;
        state += c;
    }
    
    cout << bfs(state) << endl;
    
    return 0;
}

AcWing 845. 八数码

标签:手动   iostream   表示   namespace   看到了   turn   时间   cpp   八数码   

原文地址:https://www.cnblogs.com/code-addicted/p/14406549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!