DXL数独~感觉萌萌哒~

#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;
}

  

时间: 2024-10-24 05:54:10

DXL数独~感觉萌萌哒~的相关文章

解数独算法的实现——剪枝优化

最近人工智能做个小实验,组队选了个数独游戏,顺便研究了一下.解数独感觉主流思想也就是深搜回溯了吧,优化就是各种剪枝方法. 1 引言 数独起源于18世纪初瑞士数学家欧拉等人研究的拉丁方阵(Latin Square),曾风靡日本和英国.现有解法包括基础解法:摒除法,余数法,进阶解法:区块摒除法(Locked Candidates).数组法(Subset).四角对角线(X-Wing).唯一矩形(Unique Rectangle).全双值坟墓(Bivalue Universal Grave).单数链(X

(简单) CF 44D Hyperdrive,数学。

In a far away galaxy there are n inhabited planets, numbered with numbers from 1 to n. They are located at large distances from each other, that's why the communication between them was very difficult until on the planet number 1 a hyperdrive was inv

[BZOJ4026]dC Loves Number Theory

试题描述 dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源. 给定一个长度为 n的正整数序列A,有q次询问,每次询问一段区间内所有元素乘积的φ(φ(n)代表1~n 中与n互质的数的个数) .由于答案可能很大,所以请对答案 mod 10^6 + 777. (本题强制在线,所有询问操作的l,r都需要 xor上一次询问的答案 lastans,初始时,lastans = 0) 输入 第一行,两个正整数,N,Q,表示序列的长度和询问的个数. 第二行有N 个正

(困难) CF 484E Sign on Fence,整体二分+线段树

Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence of n panels of 1 meter width and of arbitrary height. The i-th panel's height is hi meters. The adjacent planks follow without a gap between them. Afte

(中等) CF 311B Cats Transport,斜率优化DP。

Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The fee

php之路--半年之言

宝宝是2016.08.16开始自选php网络课程,俩月后顺利入职.感觉很幸运!10.19是上班的第一天,感觉萌萌哒!说一下,我是转行过来的还是蛮高兴滴.入职三月感觉一脸懵逼样,什么都不会,想到是半路出家.好吃亏,什么基础都没有.忘了入职薪资,很低,跟服务一样.2500一月.,转正3000.那是觉得能找到工作就已经满足了.时间过得挺快滴,一转眼已经是2017.05.21,今天是星期天,感慨一下已经入职7月零两天.现在感觉已经适应,勉强能修修改改,关键技术真的没有多少进步,感觉还是个白痴,什么都不懂

(中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。

In the country there are exactly n cities numbered with positive integers from 1 to n. In each city there is an airport is located. Also, there is the only one airline, which makes m flights. Unfortunately, to use them, you need to be a regular custo

(中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。

"Couple Trees" are two trees, a husband tree and a wife tree. They are named because they look like a couple leaning on each other. They share a same root, and their branches are intertwined. In China, many lovers go to the couple trees. Under t

(简单) POJ 2492 A Bug&#39;s Life,二分染色。

Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs