跳到正文

个人记录

P1219 [USACO1.5] 八皇后 Checker Challenge

15066212pp约 14 分钟
原文
文章信息与版本

文章信息

发布
2023年10月30日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 14 分钟

版本

  1. v1当前

传送门

舞蹈链

C++
#include <bits/stdc++.h>

using namespace std;

class node {
public:
    int u{}, d{}, l{}, r{}; //上下左右
    int row{}, col{};   //具体坐标
    int s{};          //col节点数量
    int h{};          //row头节点
    int ans{};        //选了那些row

    node() = default;
};

class DLX {
public:
    int n{}, m{};
    int N{}, M{};
    int num{};
    int res{};
    vector<node> nodes;
    priority_queue<vector<int>, vector<vector<int>>, greater<>> paths;

    explicit DLX(int n) {
        this->N = n;
        this->n = N * N;//n * n列
        this->m = 6 * N;//6 * n行
        this->M = geiMaxSize();
        dance();
    }

    [[nodiscard]] int getRes() const {
        return res;
    }

    vector<vector<int>> getPaths() {
        vector<vector<int>> ans;
        for (int i = 0; !paths.empty() && i < 3; i++) {
            ans.push_back(paths.top());
            paths.pop();
        }
        return ans;
    }

    [[nodiscard]] int geiMaxSize() const {
        int ans = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                ans += 4;
            }
        }
        return ans + m + 5;
    }

    void dance() {
        init();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                int id = i * N + j + 1;
                Link(id, i + 1);//N
                Link(id, j + N + 1);//N
                Link(id, i + j + (2 * N) + 1);//2*N-1
                Link(id, (N - 1 + i - j) + (4 * N - 1) + 1);
            }
        }
        Dance(0);
    }

    void init() {
        nodes.assign(M, {});

        num = m + 1;
        for (int i = 0; i <= m; i++) {
            node &it = nodes[i];
            it.l = i - 1;
            it.r = i + 1;
            it.u = it.d = i;
        }
        nodes[0].l = m;
        nodes[m].r = 0;
    }

    void Link(int r, int c) {
        num++;
        node &row = nodes[r];
        node &col = nodes[c];
        node &nums = nodes[num];

        nums.row = r;
        nums.col = c;
        col.s++;

        // node[col.u] <-> nums <-> col
        nums.u = col.u;
        nodes[col.u].d = num;
        col.u = num;
        nums.d = c;

        if (row.h == 0) {
            row.h = nums.l = nums.r = num;
        } else {
            node &head = nodes[row.h];
            //node[head.l] <-> nums <-> head
            nums.l = head.l;
            nodes[head.l].r = num;
            head.l = num;
            nums.r = row.h;
        }
    }

    void Remove(int c) {
        node &col = nodes[c];
        // node[col.l] <-> node[col.r]
        nodes[col.l].r = col.r;
        nodes[col.r].l = col.l;

        //下右
        for (int i = col.d; i != c; i = nodes[i].d) {
            for (int j = nodes[i].r; j != i; j = nodes[j].r) {
                node &it = nodes[j];
                // node[it.u] <-> node[it.d]
                nodes[it.d].u = it.u;
                nodes[it.u].d = it.d;
                nodes[it.col].s--;
            }
        }
    }

    void Resume(int c) {
        // node[col.l] -> node[c] <- node[col.r]
        node &col = nodes[c];
        nodes[col.r].l = c;
        nodes[col.l].r = c;
        //上左
        for (int i = col.u; i != c; i = nodes[i].u) {
            for (int j = nodes[i].l; j != i; j = nodes[j].l) {
                node &it = nodes[j];
                // node[it.u] -> node[j] <- node[it.d]
                nodes[it.d].u = j;
                nodes[it.u].d = j;
                nodes[it.col].s++;
            }
        }
    }

    int Dance(int dep) { // NOLINT(*-no-recursion)
        if (nodes[0].r > N) {
            vector<int> temp(N);
            for (int i = 0; i < dep; i++) {
                node &it = nodes[i];
                int r = (it.ans - 1) / N;
                int c = (it.ans - 1) % N + 1;
                temp[r] = c;
            }
            paths.push(temp);
            res++;
            return 1;
        }
        int c = nodes[0].r;
        for (int i = nodes[0].r; i <= 2 * N; i = nodes[i].r) {
            if (nodes[i].s == 0 || nodes[c].s == 0) {
                return 0;
            }
            if (nodes[i].s < nodes[c].s) {
                c = i;
            }
        }
        Remove(c);
        for (int i = nodes[c].d; i != c; i = nodes[i].d) {
            node &it = nodes[i];
            nodes[dep].ans = it.row;
            for (int j = nodes[i].r; j != i; j = nodes[j].r) {
                Remove(nodes[j].col);
            }
            Dance(dep + 1);
            for (int j = nodes[i].l; j != i; j = nodes[j].l) {
                Resume(nodes[j].col);
            }
        }
        Resume(c);
        return 0;
    }
};

void solve() {
    int n;
    cin >> n;
    DLX o(n);
    ostringstream oss;
    vector<vector<int>> ans = o.getPaths();
    for (const auto &row: ans) {
        for (const auto &col: row) {
            oss << col << " ";
        }
        oss << "\n";
    }
    oss << o.getRes();
    cout << oss.str() << endl;
}

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

    solve();
    return 0;
}

位运算

C++
#include <bits/stdc++.h>

using namespace std;
#define int long long

class Solution {
public:
    int col{};
    int t45{};
    int t135{};
    int n;
    int cnt{};
    vector<int> board;
    vector<vector<int>> ans;

    explicit Solution(int n) {
        this->n = n;
        board.assign(n, 0);
        dfs(0);
    };

    void add(int i, int j) {
        col |= 1 << j;
        t45 |= 1 << (i + j);
        t135 |= 1 << (n - 1 + i - j);
    }

    void remove(int i, int j) {
        col ^= 1 << j;
        t45 ^= 1 << (i + j);
        t135 ^= 1 << (n - 1 + i - j);
    }

    [[nodiscard]] bool contains(int i, int j) const {
        return (col & (1 << j)) != 0
               || (t45 & (1 << (i + j))) != 0
               || (t135 & (1 << (n - 1 + i - j))) != 0;
    }

    int dfs(int r) { // NOLINT(*-no-recursion)
        if (r == n) {
            if (cnt < 3) {
                ans.push_back(board);
            }
            cnt++;
            return 1;
        }
        int res = 0;
        for (int c = 0; c < n; c++) {
            if (!contains(r, c)) {
                add(r, c);
                board[r] = c;
                res += dfs(r + 1);
                board[r] = 0;
                remove(r, c);
            }
        }
        return res;
    }
};

void solve() {
    int n;
    cin >> n;
    Solution tool(n);
    ostringstream oss;
    for (auto &vec: tool.ans) {
        for (auto &i: vec) {
            oss << (i + 1) << " ";
        }
        oss << "\n";
    }
    oss << tool.cnt;
    cout << oss.str() << endl;
}

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

    solve();
    return 0;
}
原文
抓取于
  1. _Geburah

    %%%