AcWing 845. 八数码【BFS】


一、题目链接

AcWing 845. 八数码
在这里插入图片描述
在这里插入图片描述


二、题目分析

(一)算法标签

BFS

(二)解题思路

此题考查BFS
BFS框架见 https://blog.csdn.net/Derrickhang/article/details/123694440
本题整个矩阵(一个数组(字符串))是一个状态,每次扩展到它能到达的状态,同时距离+1,最终看能否找到最终状态
此题出队时判重和入队时判重都可以

详细了解搜索类题目(DFS、BFS),请移步DFS BFS 搜索题目归纳


三、AC代码

解法一:
出队时判重:

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

using namespace std;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int bfs(string start)
{
    // 相当于dist数组和st数组
    unordered_map<string, int> dist;
    dist[start] = 0;
    string end = "12345678x";
    
    queue<string> q;
    q.push(start);
    
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        
        if (t == end) return dist[end];
        
        // x所在字符串的下标
        int id = t.find('x');
        // x所在矩阵的二维坐标
        int x = id / 3, y = id % 3;
        // 距离
        int distance = dist[t];
        
        
        
        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) continue;
            int newid = a * 3 + b;
            swap(t[newid], t[id]);
            // if (t == end) return dist[end];
            if (!dist.count(t))
            {
                dist[t] = distance + 1;
                q.push(t);
            }
            swap(t[newid], t[id]);
            
        }
    }
    return -1;
}
int main()
{
    string start;
    char c;
    for (int i = 0; i < 9; i ++ )
    {
        cin >> c;
        start += c;
    }
    cout << bfs(start);
    return 0;
}

入队时判重:

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

using namespace std;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int bfs(string start)
{
    // 相当于dist数组和st数组
    unordered_map<string, int> dist;
    dist[start] = 0;
    string end = "12345678x";
    
    queue<string> q;
    q.push(start);
    
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        
        // if (t == end) return dist[end];
        
        // x所在字符串的下标
        int id = t.find('x');
        // x所在矩阵的二维坐标
        int x = id / 3, y = id % 3;
        // 距离
        int distance = dist[t];
        
        
        
        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) continue;
            int newid = a * 3 + b;
            swap(t[newid], t[id]);
            if (!dist.count(t))
            {
                dist[t] = distance + 1;
                q.push(t);
            }
            if (t == end) return dist[end];
            swap(t[newid], t[id]);
            
        }
    }
    return -1;
}
int main()
{
    string start;
    char c;
    for (int i = 0; i < 9; i ++ )
    {
        cin >> c;
        start += c;
    }
    cout << bfs(start);
    return 0;
}

四、其它题解

AcWing 845. 八数码
AcWing 845. 八数码【BFS/康托展开】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值