A*求条!!!40pts 悬3关
  • 板块P5507 机关
  • 楼主Infter
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/1 13:12
  • 上次更新2024/7/1 16:12:56
查看原帖
A*求条!!!40pts 悬3关
386547
Infter楼主2024/7/1 13:12

3MLE 3WA

#include <bits/stdc++.h>
using namespace std;

constexpr int MAXN = 4.1e6 + 5;
int a[MAXN];
int b[MAXN][4];

int st;

int dis[MAXN], pre[MAXN];
int op[MAXN];

int get(int x, int id) {
    return (x >> (id << 1)) & 3;
}

int calc(int x, int id, int v) {
    return x ^ ((1ll * (v & 3)) << (id << 1));
}

double h(int x) {
    double res = 0;
    for (int i = 0; i < 12; i++) {
        res += ((4 - get(x, i)) & 3) / 2.0;
    }
    return res;
}

void Ax() {
    // cerr << h(st) << endl;
    memset(pre, 0xff, sizeof pre);
    priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> q;
    q.push(make_pair(h(st) + dis[st], st));

    while (q.size()) {
        auto [f, x] = q.top();
        // cerr << f << " " << x << endl;
        q.pop();
        if (!x)
            return;

        for (int i = 0; i < 12; i++) {
            int now = get(x, i);
            int j = b[i][now];
            int then = get(x, j);
            int res = x;
            res = calc(res, i, now);
            res = calc(res, i, now + 1);
            res = calc(res, j, then);
            res = calc(res, j, then + 1);
            if (!(~pre[res])) {
                op[res] = i + 1;
                dis[res] = dis[x] + 1;
                pre[res] = x;
                q.push(make_pair(h(res) + dis[res], res));
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    for (int i = 0; i < 12; i++) {
        cin >> a[i];
        a[i]--;
        st |= (a[i] << (i << 1));
        for (int j = 0; j < 4; j++) {
            cin >> b[i][j];
            b[i][j]--;
        }
    }

    Ax();

    cout << dis[0] << endl;
    vector<int> st;
    for (int i = 0; ~i; i = pre[i]) {
        if (op[i] != 0)
        st.push_back(op[i]);
        // cerr << i << endl;
    }
    while (st.size()) {
        cout << st.back() << " ";
        st.pop_back();
    }
    cout << endl;

    return 0;
}
2024/7/1 13:12
加载中...