八数码25分,为什么我的步数比它给的还少啊??
查看原帖
八数码25分,为什么我的步数比它给的还少啊??
833124
BIOS楼主2023/1/28 20:49
#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;
struct node
{
    string s;
    int step;
};
int main()
{
    queue<node> q;
    int pos;
    map<string, bool> ac;
    node now, next;
    cin >> now.s;
    ac[now.s] == 1;
    now.step = 0;
    q.push(now);
    while (!q.empty())
    {
        now = q.front();
        q.pop();
        if (now.s == "123804765")
        {
            cout << now.step << endl;
            return 0;
        }
        pos = now.s.find('0');
        next.step = now.step + 1;
        if (pos - 1 >= 0)
        {
            swap(now.s[pos], now.s[pos - 1]); // 零左移
            if (ac[now.s] == 0)
            {
                next.s = now.s;
                ac[now.s] = 1;
                q.push(next);
            }
            swap(now.s[pos], now.s[pos - 1]);
        }
        if (pos + 1 <= 8)
        {
            swap(now.s[pos], now.s[pos + 1]); // 零右移
            if (ac[now.s] == 0)
            {
                next.s = now.s;
                ac[now.s] = 1;
                q.push(next);
            }
            swap(now.s[pos], now.s[pos + 1]);
        }
        if (pos - 3 >= 0)
        {
            swap(now.s[pos], now.s[pos - 3]); // 零上移
            if (ac[now.s] == 0)
            {
                next.s = now.s;
                ac[now.s] = 1;
                q.push(next);
            }
            swap(now.s[pos], now.s[pos - 3]);
        }
        if (pos + 3 <= 8)
        {
            swap(now.s[pos], now.s[pos + 3]); // 零下移
            if (ac[now.s] == 0)
            {
                next.s = now.s;
                ac[now.s] = 1;
                q.push(next);
            }
            swap(now.s[pos], now.s[pos + 3]);
        }
    }
}

发生什么事了? 下载了一个数据,发现我的代码跑出来的步数比它还少,我的思路有问题吗这是????

2023/1/28 20:49
加载中...