舞蹈链(Dancing Links)求解数独

讲解(写的太好了orz

POJ - 3074(3 * 3)

题意:求3 * 3 * 3大小的数独

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int K = 3;//宫的大小
const int N = 9;//每行多少数即3 * 3
const int MAXN = N * N * N + 10;
const int MAXM = N * N * 4 + 10;
const int maxnode = MAXN * 4 + MAXM + 10;
char g[MAXN];

struct DLX {
    int n, m, siz;
    int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
    int H[MAXN], S[MAXM];
    int ansd, ans[MAXN];
    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for(int i = 0; i <= m; ++i) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0, L[0] = m;
        siz = m;
        for(int i = 1; i <= n; ++i) H[i] = -1;
    }
    void Link(int r, int c) {
        ++S[Col[++siz] = c];
        Row[siz] = r;
        D[siz] = D[c];
        U[D[c]] = siz;
        U[siz] = c;
        D[c] = siz;
        if(H[r] < 0) H[r] = L[siz] = R[siz] = siz;
        else {
            R[siz] = R[H[r]];
            L[R[H[r]]] = siz;
            L[siz] = H[r];
            R[H[r]] = siz;
        }
    }
    void remove(int c) {
        L[R[c]] = L[c], R[L[c]] = R[c];
        for(int i = D[c]; i != c; i = D[i]) {
            for(int j = R[i]; j != i; j = R[j]) {
                U[D[j]] = U[j];
                D[U[j]] = D[j];
                --S[Col[j]];
            }
        }
    }
    void resume(int c) {
        for(int i = U[c]; i != c; i = U[i])
            for(int j = L[i]; j != i; j = L[j])
                ++S[Col[U[D[j]] = D[U[j]] = j]];
        L[R[c]] = R[L[c]] = c;
    }
    bool Dance(int d) {
        if(R[0] == 0) {
            for(int i = 0; i < d; ++i) g[(ans[i] - 1) / N] = (ans[i] - 1) % N + '1';    ///输出的数字
            for(int i = 0; i < N * N; ++i) printf("%c", g[i]);
            printf("\n");
            return 1;
        }
        int c = R[0];
        for(int i = R[0]; i != 0; i = R[i])
            if(S[i] < S[c]) c = i;
        remove(c);
        for(int i = D[c]; i != c; i = D[i]) {
            ans[d] = Row[i];
            for(int j = R[i]; j != i; j = R[j]) remove(Col[j]);
            if(Dance(d + 1)) return 1;
            for(int j = L[i]; j != i; j = L[j]) resume(Col[j]);
        }
        resume(c);
        return 0;
    }
};

void place(int &r, int &c1, int &c2, int &c3, int &c4, int i, int j, int k) {
    r = (i * N + j) * N + k;
    c1 = i * N + j + 1;
    c2 = N * N + i * N + k;
    c3 = N * N * 2 + j * N + k;
    c4 = N * N * 3 + ((i / K) * K + (j / K)) * N + k;
}

DLX dlx;

int main() {
    while(~scanf("%s", g)) {
        if(strcmp(g, "end") == 0) break;
        dlx.init(N * N * N, N * N * 4);
        int r, c1, c2, c3, c4;
        for(int i = 0; i < N; ++i) {
            for(int j = 0; j < N; ++j) {
                for(int k = 1; k <= N; ++k) {
                    ///根据题目给出的具体形式改动
                    if(g[i * N + j] == '.' || g[i * N + j] == '0' + k) {
                        place(r, c1, c2, c3, c4, i, j, k);
                        dlx.Link(r, c1);
                        dlx.Link(r, c2);
                        dlx.Link(r, c3);
                        dlx.Link(r, c4);
                    }
                }
            }
        }
        dlx.Dance(0);
    }
    return 0;
}

ZOJ - 3122(4 * 4)

题意:求4 * 4 * 4的数独

(题目中的样例不对,以下是正确样例,注意每两个数独之间输出换行,最后一个数独不用额外换行

--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int K = 4;//宫的大小
const int N = 16;//每行多少数即4*4
const int MAXN = N * N * N + 10;
const int MAXM = N * N * 4 + 10;
const int maxnode = MAXN * 4 + MAXM + 10;
char g[MAXN];

struct DLX {
    int n, m, siz;
    int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
    int H[MAXN], S[MAXM];
    int ansd, ans[MAXN];
    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for(int i = 0; i <= m; ++i) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0, L[0] = m;
        siz = m;
        for(int i = 1; i <= n; ++i) H[i] = -1;
    }
    void Link(int r, int c) {
        ++S[Col[++siz] = c];
        Row[siz] = r;
        D[siz] = D[c];
        U[D[c]] = siz;
        U[siz] = c;
        D[c] = siz;
        if(H[r] < 0) H[r] = L[siz] = R[siz] = siz;
        else {
            R[siz] = R[H[r]];
            L[R[H[r]]] = siz;
            L[siz] = H[r];
            R[H[r]] = siz;
        }
    }
    void remove(int c) {
        L[R[c]] = L[c], R[L[c]] = R[c];
        for(int i = D[c]; i != c; i = D[i]) {
            for(int j = R[i]; j != i; j = R[j]) {
                U[D[j]] = U[j];
                D[U[j]] = D[j];
                --S[Col[j]];
            }
        }
    }
    void resume(int c) {
        for(int i = U[c]; i != c; i = U[i])
            for(int j = L[i]; j != i; j = L[j])
                ++S[Col[U[D[j]] = D[U[j]] = j]];
        L[R[c]] = R[L[c]] = c;
    }
    bool Dance(int d) {
        if(R[0] == 0) {
            for(int i = 0; i < d; ++i) g[(ans[i] - 1) / N] = (ans[i] - 1) % N + 'A';    ///输出的数字
            for(int i = 0; i < N * N; ++i) {
                if(i && i % N == 0) printf("\n");
                printf("%c", g[i]);
            }
            printf("\n");
            return 1;
        }
        int c = R[0];
        for(int i = R[0]; i != 0; i = R[i])
            if(S[i] < S[c]) c = i;
        remove(c);
        for(int i = D[c]; i != c; i = D[i]) {
            ans[d] = Row[i];
            for(int j = R[i]; j != i; j = R[j]) remove(Col[j]);
            if(Dance(d + 1)) return 1;
            for(int j = L[i]; j != i; j = L[j]) resume(Col[j]);
        }
        resume(c);
        return 0;
    }
};

void place(int &r, int &c1, int &c2, int &c3, int &c4, int i, int j, int k) {
    r = (i * N + j) * N + k;
    c1 = i * N + j + 1;
    c2 = N * N + i * N + k;
    c3 = N * N * 2 + j * N + k;
    c4 = N * N * 3 + ((i / K) * K + (j / K)) * N + k;
}

DLX dlx;

int main() {
    bool flag = 0;
    while(~scanf("%s", g)) {
        if(flag) printf("\n");
        flag = 1;
        for(int i = 1; i < N; ++i) scanf("%s", g + i * N);
        dlx.init(N * N * N, N * N * 4);
        int r, c1, c2, c3, c4;
        for(int i = 0; i < N; ++i) {
            for(int j = 0; j < N; ++j) {
                for(int k = 1; k <= N; ++k) {
                    ///根据题目给出的具体形式改动
                    if(g[i * N + j] == '-' || g[i * N + j] == 'A' + k - 1) {
                        place(r, c1, c2, c3, c4, i, j, k);
                        dlx.Link(r, c1);
                        dlx.Link(r, c2);
                        dlx.Link(r, c3);
                        dlx.Link(r, c4);
                    }
                }
            }
        }
        dlx.Dance(0);
    }
    return 0;
}

/*
--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值