POJ 3076 Sudoku (DLX解數獨)

題目大意:

解一個16*16形式的數獨

要求每行每列每個16宮格都包含A~P


大致思路:

和POJ 3074一個道理...繼續練手

剛開始數組開小調了一段時間...


代碼如下:

Result  :  Accepted     Memory  :  500 KB     Time  :  3391 ms

/*
 * Author: Gatevin
 * Created Time:  2015/10/2 8:52:27
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

/*
 * DLX解經典數獨問題
 * 考慮到每行都要有1~16
 * 用第1~16*16列表示第i行是否有數字j(第(i - 1)*9 + j列的狀態)
 * 由於每列都要有1~16
 * 用第16*16 + 1 ~  16*16*2列表示第i列是否有數字j (第(i - 1)*16 + j + 16*16列的狀態)
 * 考慮到每個九宮格都要有1~16
 * 用第16*16+1~16*16*2列表示第i個九宮格是否有數字j (第(i - 1)*16 + j + 16*16列的狀態)
 * 但是僅僅依靠這些是不夠的
 * 還有一個限制條件每個格子只能有1個數字
 * 所以用第16*16*2+1~16*16*3列表示格子中是否填了數
 * 那麼對於給定的數字, 就在對應的列上寫4個1
 * 如果是.那麼就添加16行, 分別對應哪個位子填1~16的情況
 * 那麼最壞情況下有16*16*16行可選
 */

char in[800];

struct DLX
{
#define maxn 16*16*16 + 4
#define maxm 16*16*4 + 4
#define maxnode 16*16*16*4 + 100
    int n, m, size;
    int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
    int H[maxn], S[maxm];
    int ansd, ans[maxn];
    struct State
    {
        int x, y, val;
        State(int _x, int _y, int _val)
        {
            x = _x, y = _y, val = _val;
        }
        State(){}
    };
    State s[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;
        size = m;
        for(int i = 1; i <= n; i++) H[i] = -1;
    }
    void Link(int r, int c)
    {
        ++S[Col[++size] = c];
        Row[size] = r;
        D[size] = D[c];
        U[D[c]] = size;
        U[size] = c;
        D[c] = size;
        if(H[r] < 0) H[r] = L[size] = R[size] = size;
        else
        {
            R[size] = R[H[r]];
            L[R[H[r]]] = size;
            L[size] = H[r];
            R[H[r]] = size;
        }
    }
    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 dep)
    {
        if(R[0] == 0)
        {
            ansd = dep;
            return true;
        }
        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[dep] = Row[i];
            for(int j = R[i]; j != i; j = R[j]) remove(Col[j]);
            if(Dance(dep + 1)) return true;
            for(int j = L[i]; j != i; j = L[j]) resume(Col[j]);
        }
        resume(c);
        return false;
    }
    void solve()
    {
        int tn = 0, tm = 16*16*4;
        int len = strlen(in);
        for(int i = 0; i < len; i++)
            if(in[i] == '-') tn += 16;
            else tn++;
        init(tn, tm);
        tn = 0;
        for(int i = 0; i < 16; i++)
            for(int j = 0; j < 16; j++)
            {
                char c = in[i*16 + j];
                if(c == '-')
                {
                    for(int k = 1; k <= 16; k++)
                    {
                        s[++tn] = State(i, j, k);
                        Link(tn, i*16 + k);
                        Link(tn, j*16 + k + 16*16);
                        Link(tn, (i/4 * 4 + j / 4)*16 + k + 16*16*2);
                        Link(tn, i*16 + j + 1 + 16*16*3);
                    }
                }
                else
                {
                    s[++tn] = State(i, j, c - 'A' + 1);
                    Link(tn, i*16 + c - 'A' + 1);
                    Link(tn, j*16 + c - 'A' + 1 + 16*16);
                    Link(tn, (i/4 * 4 + j / 4)*16 + c - 'A' + 1 + 16*16*2);
                    Link(tn, i*16 + j + 1 + 16*16*3);
                }
            }
        if(!Dance(0)) puts("No answer");
        else
        {
            for(int i = ansd - 1; i >= 0; i--)
            {
                int sel = ans[i];
                in[s[sel].x*16 + s[sel].y] = s[sel].val + 'A' - 1 ;
            }
            for(int i = 0; i < 16*16; i++)
            {
                putchar(in[i]);
                if(i % 16 == 15) putchar('\n');
            }
        }
    }
};

DLX dlx;

int main()
{
    bool fir = 1;//每組數據間要空行不然會PE...
    while(scanf("%s", in) != EOF)
    {
        //if(strcmp(in, "end") == 0) break;
        if(!fir) putchar('\n');
        fir = 0;
        for(int i = 1; i < 16; i++)
            scanf("%s", in + i*16);
        dlx.solve();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章