-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker_sd.c
64 lines (59 loc) · 1.57 KB
/
checker_sd.c
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
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "checker_sd.h"
// allocate a sudoku board on stack
sd_check_t alloc_sd(sz_t n) {
sd_check_t sd;
sd.n = n;
sd.ne2 = sd.n * sd.n;
sd.ne4 = sd.ne2 * sd.ne2;
sd.board = malloc(sizeof(int) * sd.ne4);
assert(sd.board != NULL);
return sd;
}
// deallocate the malloced bit
void clear_sd(sd_check_t sd) {
free(sd.board);
}
checker_res check_list(sz_t n, int *a) {
char values[n*n];
memset(values, 0x00, n * sizeof(char));
checker_res ret = COMPLETE;
for(sz_t i = 0; i < n; ++i) {
if(a[i] == 0) {
assert(ret != INVALID);
if(ret == COMPLETE)
ret = INCOMPLETE;
continue;
}
if(values[a[i] - 1])
return INVALID;
else
values[a[i] - 1] = true;
}
return ret;
}
#define MIN(a, b) ((a) < (b) ? (a) : (b))
checker_res check_sudoku(sd_check_t sd) {
int arrays[sd.ne2 * CONSTRSIZE];
checker_res res = COMPLETE;
for(sz_t i = 0; i < sd.ne2; ++i) {
for(sz_t j = 0; j < sd.ne2; ++j) {
arrays[j + sd.ne2*ROW] = sd.board[(sd.n * (i / sd.n) + (j / sd.n)) * sd.ne2 + (sd.n * (i % sd.n) + (j % sd.n))];
arrays[j + sd.ne2*COL] = sd.board[i * sd.ne2 + j];
arrays[j + sd.ne2*BOX] = sd.board[j * sd.ne2 + i];
}
res = MIN(res, check_list(sd.ne2, arrays + sd.ne2*ROW));
if(res == INVALID)
return res;
res = MIN(res, check_list(sd.ne2, arrays + sd.ne2*COL));
if(res == INVALID)
return res;
res = MIN(res, check_list(sd.ne2, arrays + sd.ne2*BOX));
if(res == INVALID)
return res;
}
return res;
}