码迷,mamicode.com
首页 > 其他好文 > 详细

DXL数独~感觉萌萌哒~

时间:2015-04-23 23:13:56      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int M = 100005;
const int N = 16;
struct DLX{
    int D[M], U[M], R[M], L[M], head[5000], S[5000], col[M], row[M];
    int ans[5025];
    int sz, ansd;
    void init(int tn){
        sz = tn;
        for(int i = 0; i <= tn; i ++) {
            D[i] = U[i] = i;
            L[i] = i-1; R[i] = i+1;
        } L[0] = tn; R[tn] = 0;
        memset(S, 0, sizeof(S));
        memset(head, -1, sizeof(head));
        head[0] = 0;
    }
    void Insert(int r, int c){
        ++S[col[++sz] = c];
        row[sz] = r;
        D[sz] = D[c];
        U[D[c]] = sz;
        U[sz] = c;
        D[c] = sz;
        if(head[r] < 0) head[r] = L[sz] = R[sz] = sz;
        else{
            R[sz] = R[head[r]];
            L[R[head[r]]] = sz;
            L[sz] = head[r];
            R[head[r]] = sz;
        }
    }
    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){
            ansd = d;
            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[d] = row[i];
            for(int j = R[i]; j != i; j = R[j]) {
                remove(col[j]);
            }
            if(Dance(d+1)) return true;
            for(int j = L[i]; j != i; j = L[j]) resume(col[j]);
        }
        resume(c);
        return false;
    }
} g;

char s[17][18];
void print(){
    for(int i = 0; i < g.ansd; i ++){
        g.ans[i] --;
        int num = g.ans[i] % N; int y = g.ans[i]/N%N;
        int x = g.ans[i] /N/N;
        s[x][y] = num + ‘A‘;
    }
    for(int i = 0; i < N; i ++) printf("%s\n", s[i]);
}

int main(){
    int f = 1;
    while(scanf("%s", s[0]) != EOF){
        if(f) f = 0;
        else printf("\n");
        for(int i = 1; i < N; i ++) scanf("%s", s[i]);
        g.init(1024);
        for(int i = 0; i < N; i ++){
            for(int j = 0; j < N; j ++){
                for(int o = 0; o < N; o ++){
                    if(s[i][j] == ‘-‘ || s[i][j] == ‘A‘ + o){
                        g.Insert((i*N+j)*N+o+1, i*N+j+1);
                        g.Insert((i*N+j)*N+o+1, N*N+o+i*N+1);
                        g.Insert((i*N+j)*N+o+1, N*N*2+o+j*N+1);
                        g.Insert((i*N+j)*N+o+1, N*N*3+o+((i/4)*4+j/4)*N+1);
                    }
                }
            }
        }
        bool flag = g.Dance(0) ;
        print();
    }
    return 0;
}

  

DXL数独~感觉萌萌哒~

标签:

原文地址:http://www.cnblogs.com/acmood/p/4451982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!