b112困難

110 新北市資訊學科能力競賽複賽(2021)|雙洞推盤遊戲

以可證下界搜尋全盤最少移動數。

解題思路

把9,10,11與12,13,14分為兩個模式,各保留三張牌的位置及兩個洞,其他牌不區分。從目標做0-1 BFS:模式內牌移動代價1,其他牌移動代價0。每模式有16P5/2=262080合法狀態,以五個四位元位置編碼;上排格完全保留,可自由移動。

一次實際移動至多被其中一模式收費;若移的是1至8,兩模式均收費0。因此兩表距離加上1至8的曼哈頓距離,是可相加的完整問題下界。若上兩排正確,可再用「完全不動上排的精確步數」與「模式下界加至少兩次上排移動」取小值,強化下界,這是對兩類完整解的分類。

底部2×4 BFS只提供合法方案上界;完整4×4 IDA*仍枚舉任何牌移入任一相鄰洞,逐步提高g+h門檻。只剪立即反向步、超下界門檻及同盤面較長到達。首次找到解或證明沒有比上界更短的路,就得到真正最少步數。

兩模式各O(16P5)前處理,編碼表各2^20位元組;底部BFS20160狀態。IDA*最壞指數時間,路徑與當輪轉置表依實際展開量使用空間,沒有縮盤面或截斷深度。

Step-by-step animation

加法式Pattern Database與完整4×4 IDA*|上排1至8正確;底兩排9 10 11 12/13 0 14 0。

回報動畫錯誤
步驟 1 / 3

起盤

上排1至8正確;底兩排9 10 11 12/13 0 14 0。

9 10 11 12
13 0 14 0
尚差
14向左1格

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
array<vector<int>, 16> adj;
vector<unsigned char> build(array<int, 3> goal) {
    vector<unsigned char> d(1 << 20, 255), done(1 << 20);
    int start = goal[0] | goal[1] << 4 | goal[2] << 8 | 14 << 12 | 15 << 16;
    d[start] = 0;
    deque<int> q{start};
    while (q.size()) {
        int code = q.front();
        q.pop_front();
        if (done[code])
            continue;
        done[code] = 1;
        int p0 = code & 15, p1 = code >> 4 & 15, p2 = code >> 8 & 15,
            h1 = code >> 12 & 15, h2 = code >> 16 & 15, value = d[code];
        for (auto [h, other] : vector<pair<int, int>>{{h1, h2}, {h2, h1}})
            for (int x : adj[h]) {
                if (x == other)
                    continue;
                int a = p0, b = p1, c = p2, cost = 0;
                if (x == a) {
                    a = h;
                    cost = 1;
                } else if (x == b) {
                    b = h;
                    cost = 1;
                } else if (x == c) {
                    c = h;
                    cost = 1;
                }
                int lo = min(x, other), hi = max(x, other),
                    key = a | b << 4 | c << 8 | lo << 12 | hi << 16, nv = value + cost;
                if (nv < d[key]) {
                    d[key] = nv;
                    if (cost)
                        q.push_back(key);
                    else
                        q.push_front(key);
                }
            }
    }
    return d;
}
int main() {
    array<int, 16> board;
    array<int, 15> pos{};
    vector<int> holes;
    for (int i = 0; i < 16; i++) {
        cin >> board[i];
        if (board[i])
            pos[board[i]] = i;
        else
            holes.push_back(i);
        int x = i / 4, y = i % 4;
        for (auto [dx, dy] : vector<pair<int, int>>{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {
            int a = x + dx, b = y + dy;
            if (a >= 0 && a < 4 && b >= 0 && b < 4)
                adj[i].push_back(a * 4 + b);
        }
    }
    bool solved = true;
    for (int i = 0; i < 14; i++)
        if (board[i] != i + 1)
            solved = false;
    if (solved) {
        cout << 0 << "\n";
        return 0;
    }
    auto db1 = build({8, 9, 10}), db2 = build({11, 12, 13});
    auto bottomCode = [&]() {
        int key = 0;
        for (int i = 8; i < 16; i++)
            key |= (board[i] ? board[i] - 8 : 0) << (3 * (i - 8));
        return key;
    };
    auto unpack = [](int key) {
        array<int, 8> a{};
        for (int i = 0; i < 8; i++)
            a[i] = (key >> (3 * i)) & 7;
        return a;
    };
    int goal = 0;
    for (int i = 0; i < 6; i++)
        goal |= (i + 1) << (3 * i);
    unordered_map<int, int> bottom;
    bottom[goal] = 0;
    queue<int> q;
    q.push(goal);
    while (q.size()) {
        int key = q.front();
        q.pop();
        auto a = unpack(key);
        int value = bottom[key];
        for (int h = 0; h < 8; h++)
            if (!a[h])
                for (int vv : adj[h + 8])
                    if (vv >= 8) {
                        int v = vv - 8;
                        if (a[v]) {
                            int next = key ^ (a[v] << (3 * h)) ^ (a[v] << (3 * v));
                            if (!bottom.count(next)) {
                                bottom[next] = value + 1;
                                q.push(next);
                            }
                        }
                    }
    }
    int upper = bottom[bottomCode()], md[15][16] = {};
    for (int t = 1; t <= 14; t++)
        for (int u = 0; u < 16; u++)
            md[t][u] = abs(u / 4 - (t - 1) / 4) + abs(u % 4 - (t - 1) % 4);
    auto heuristic = [&](int h1, int h2, int top) {
        int common = h1 << 12 | h2 << 16;
        int h = db1[pos[9] | pos[10] << 4 | pos[11] << 8 | common] +
                db2[pos[12] | pos[13] << 4 | pos[14] << 8 | common] + top;
        if (!top)
            h = max(h, min(bottom[bottomCode()], h + 2));
        return h;
    };
    using U = unsigned long long;
    U initial = 0;
    for (int i = 0; i < 16; i++)
        initial |= U(board[i]) << (4 * i);
    unordered_map<U, int> seen;
    function<int(int, int, int, int, int, U, int, int)> dfs =
        [&](int g, int bound, int h1, int h2, int top, U code, int avoidH, int avoidV) {
            int h = heuristic(h1, h2, top);
            if (g + h > bound)
                return g + h;
            if (!h)
                return -1;
            if (!top && g + bottom[bottomCode()] <= bound)
                return -1;
            int best = 100;
            vector<array<int, 7>> options;
            for (auto [hole, other] : vector<pair<int, int>>{{h1, h2}, {h2, h1}})
                for (int v : adj[hole]) {
                    int tile = board[v];
                    if (!tile || (hole == avoidH && v == avoidV))
                        continue;
                    int a = min(v, other), b = max(v, other),
                        nt = top + (tile <= 8 ? md[tile][hole] - md[tile][v] : 0);
                    board[hole] = tile;
                    board[v] = 0;
                    pos[tile] = hole;
                    int nh = heuristic(a, b, nt);
                    board[v] = tile;
                    board[hole] = 0;
                    pos[tile] = v;
                    if (g + 1 + nh <= bound)
                        options.push_back({nh, hole, v, tile, a, b, nt});
                    else
                        best = min(best, g + 1 + nh);
                }
            sort(options.begin(), options.end());
            for (auto op : options) {
                auto [hh, hole, v, tile, a, b, nt] = op;
                U nc = code ^ (U(tile) << (4 * hole)) ^ (U(tile) << (4 * v));
                int ng = g + 1;
                auto it = seen.find(nc);
                if (it != seen.end() && it->second <= ng)
                    continue;
                seen[nc] = ng;
                board[hole] = tile;
                board[v] = 0;
                pos[tile] = hole;
                int result = dfs(ng, bound, a, b, nt, nc, v, hole);
                board[v] = tile;
                board[hole] = 0;
                pos[tile] = v;
                if (result == -1)
                    return -1;
                best = min(best, result);
            }
            return best;
        };
    int bound = heuristic(holes[0], holes[1], 0);
    while (bound < upper) {
        seen.clear();
        seen[initial] = 0;
        int result = dfs(0, bound, holes[0], holes[1], 0, initial, -1, -1);
        if (result == -1) {
            cout << bound << "\n";
            return 0;
        }
        bound = result;
    }
    cout << upper << "\n";
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多