acwing 1107 魔板 (最小步数模型)

题面

acwing 1107 魔板 (最小步数模型)_第1张图片

题解

和八数码问题类似,都是将初始状态变化成最终状态所花费的最小步数,我们可以将每个状态看成一个点进行更新即可

代码

#include
#include
#include
#include
#include
#include
#include

using namespace std;

char g[2][4];
unordered_map<string, pair<char, string>> pre;
unordered_map<string, int> dist;

void set(string state) {
    for (int i = 0; i < 4; i++) g[0][i] = state[i];
    for (int i = 0, j = 7; i < 4; i++) g[1][i] = state[j--];
}

string get() {
    string res;
    for (int i = 0; i < 4; i++) res += g[0][i];
    for (int i = 3; i >= 0; i--) res += g[1][i];
    return res;
}

string moveA(string state) {
    set(state);
    for (int i = 0; i < 4; i++) swap(g[0][i], g[1][i]);
    return get();
}

string moveB(string state) {
    set(state);
    char temp0 = g[0][3], temp1 = g[1][3];
    for (int i = 3; i >= 0; i--) {
        g[0][i] = g[0][i - 1];
        g[1][i] = g[1][i - 1];
    }
    g[0][0] = temp0, g[1][0] = temp1;
    return get();

}

string moveC(string state) {
    set(state);
    char temp = g[0][1];
    g[0][1] = g[1][1];
    g[1][1] = g[1][2];
    g[1][2] = g[0][2];
    g[0][2] = temp;
    return get();
}


int bfs(string strat, string end) {
    if (strat == end) return 0;
    queue<string> q;
    q.push(strat);
    dist[strat] = 0;
    while (q.size()) {
        auto t = q.front();
        q.pop();
        string m[3];
        m[0] = moveA(t), m[1] = moveB(t), m[2] = moveC(t);
        for (int i = 0; i < 3; i++) {
            if (!dist.count(m[i])) {
                dist[m[i]] = dist[t] + 1;
                pre[m[i]] = {char(i + 'A'), t};
                q.push(m[i]);
                if (m[i] == end) return dist[m[i]];
            }
        }
    }
    return -1;
}

int main() {

    string start, end;
    for (int i = 1; i <= 8; i++) start += char(i + '0');
    int x;
    for (int i = 1; i <= 8; i++) {
        cin >> x;
        end += char(x + '0');
    }
    int step = bfs(start, end);
    cout << step << endl;
    string res;
    while (end != start) {
        res += pre[end].first;
        end = pre[end].second;
    }
    reverse(res.begin(), res.end());
    if (step) cout << res << endl;

    return 0;
}

你可能感兴趣的:(搜索,BFS)