summaryrefslogtreecommitdiff
path: root/95-99/sudoku.c
blob: f8591f73e977ef14247671adae617e5a2e70259a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define SUDOKU_N 3
#define BLOCK_N (SUDOKU_N * SUDOKU_N)
#define DIGITS_N BLOCK_N
#define PLACE_BIT_N (BLOCK_N * BLOCK_N)
#define BLOCK_BIT_N (BLOCK_N * DIGITS_N)
#define ROW_BIT_N  BLOCK_BIT_N
#define COL_BIT_N ROW_BIT_N
#define SET_COVER_ROW (BLOCK_N * BLOCK_N * DIGITS_N)
#define SET_COVER_COL (PLACE_BIT_N + BLOCK_BIT_N + ROW_BIT_N + COL_BIT_N)

typedef struct DNode {
    struct DNode *left, *right, *up, *down;
    struct DNode *ctl;
    uint32_t size, row;
} DNode; /* the node of dlx */

int bitmap[SET_COVER_ROW][SET_COVER_COL];
int row_seq[SET_COVER_ROW];

int puzzle[BLOCK_N][BLOCK_N];
int backref[SET_COVER_ROW][3];

DNode *dnode_new() {
    return (DNode *)malloc(sizeof(DNode));
}

void dnode_free(DNode *p) {
    free(p);
}

DNode *build_dlx(int nrow, int ncol) {
    DNode *head = dnode_new();
    DNode *chead[SET_COVER_COL];
    head->left = head->right = head;
    head->up = head->down = head;
    int i, j;
    /* build the guard chain */
    for (j = 0; j < ncol; j++)
    {
        DNode *p = chead[j] = dnode_new();
        (p->left = head->left)->right = p;
        (p->right = head)->left = p;
        p->up = p->down = p;
    }
    DNode *rhead = dnode_new(); /* helper node */
    for (i = 0; i < nrow; i++)
    {
        rhead->left = rhead->right = rhead;
        for (j = 0; j < ncol; j++)
            if (bitmap[i][j])
            {
                DNode *p = dnode_new();
                p->ctl = chead[j];
                p->row = i;
                (p->left = rhead->left)->right = p;
                (p->right = rhead)->left = p;
                (p->up = p->ctl->up)->down = p;
                (p->down = p->ctl)->up = p;
            }
        rhead->left->right = rhead->right;
        rhead->right->left = rhead->left;
    }
    dnode_free(rhead);
    return head;
}

#define LOOP(q, p, d) for (q = p->d; q != p; q = q->d)

void set_cover(DNode *pctl) {
    DNode *q, *r;
    pctl->left->right = pctl->right;
    pctl->right->left = pctl->left;
    LOOP(q, pctl, down)
        LOOP(r, q, right)
        {
            r->up->down = r->down;
            r->down->up = r->up;
            r->ctl->size--;
        }
}

void set_uncover(DNode *pctl) {
    DNode *q, *r;
    pctl->left->right = pctl;
    pctl->right->left = pctl;
    LOOP(q, pctl, up)
        LOOP(r, q, left)
        {
            r->up->down = r;
            r->down->up = r;
            r->ctl->size++;
        }
}

void dlx_print(int step) {
    int i;
    for (i = 0; i < step; i++)
        printf("%d ", row_seq[i]);
    puts("");
}

void sudoku_print(int step) {
    int i, j;
    for (i = 0; i < step; i++)
    {
        int *t = backref[row_seq[i]];
        puzzle[t[0]][t[1]] = t[2];
    }
    for (i = 0; i < BLOCK_N; i++, puts(""))
        for (j = 0; j < BLOCK_N; j++)
            printf("%d ", puzzle[i][j]);
    puts("");
}

void search(DNode *head, int step) {
    DNode *p, *q, *lp;
    uint32_t least_value = -1U;
    if (head->left == head)
    {
        sudoku_print(step);
        /* dlx_print(step); */
        return;
    }
    LOOP(p, head, right)
        if (p->size < least_value)
        {
            least_value = p->size;
            lp = p;
        }

    set_cover(lp);
    LOOP(p, lp, down)
    {
        LOOP(q, p, right) set_cover(q->ctl);
        row_seq[step] = q->row;
        search(head, step + 1);
        LOOP(q, p, left) set_uncover(q->ctl);
    }
    set_uncover(lp);
}

void test_dlx() {
    int nrow, ncol, i, j;
    DNode *head;
    scanf("%d %d", &nrow, &ncol);
    for (i = 0; i < nrow; i++)
        for (j = 0; j < ncol; j++)
            scanf("%d", bitmap[i] + j);
    head = build_dlx(nrow, ncol);
    search(head, 0);
}

void sudoku() {
    int i, j, d, nrow = 0;
    DNode *head;
    for (i = 0; i < BLOCK_N; i++)
        for (j = 0; j < BLOCK_N; j++)
            scanf("%d", puzzle[i] + j);
    for (i = 0; i < BLOCK_N; i++)
        for (j = 0; j < BLOCK_N; j++)
            for (d = 0; d < DIGITS_N; d++)
                if (!puzzle[i][j] || puzzle[i][j] == d + 1)
                {
                    int *b = bitmap[nrow];
                    b[i * BLOCK_N + j] = 1;
                    b += PLACE_BIT_N;
                    b[((i / SUDOKU_N) * SUDOKU_N + j / SUDOKU_N) * BLOCK_N + d] = 1;
                    b += BLOCK_BIT_N;
                    b[i * BLOCK_N + d] = 1;
                    b += ROW_BIT_N;
                    b[j * BLOCK_N + d] = 1;
                    backref[nrow][0] = i;
                    backref[nrow][1] = j;
                    backref[nrow][2] = d + 1;
                    nrow++;
                }
    head = build_dlx(nrow, SET_COVER_COL);
    search(head, 0);
}

int main() {
    /* test_dlx(); */
    sudoku();
    return 0;
}