-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.c
174 lines (148 loc) · 3.81 KB
/
sudoku.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
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t** Grid;
const uint8_t grid[9][9] = {
{8,0,0, 0,0,0, 0,0,0},
{0,0,3, 6,0,0, 0,0,0},
{0,7,0, 0,9,0, 2,0,0},
{0,5,0, 0,0,7, 0,0,0},
{0,0,0, 0,4,5, 7,0,0},
{0,0,0, 1,0,0, 0,3,0},
{0,0,1, 0,0,0, 0,6,8},
{0,0,8, 5,0,0, 0,1,0},
{0,9,0, 0,0,0, 4,0,0}
};
const uint8_t grid2[9][9] = {
{5,9,0, 0,0,0, 7,0,0},
{0,0,0, 0,9,0, 0,0,1},
{0,0,4, 6,0,0, 0,2,0},
{2,0,5, 0,0,7, 0,0,0},
{0,0,0, 9,2,3, 0,0,0},
{0,0,0, 5,0,0, 2,0,8},
{0,3,0, 0,0,1, 5,0,0},
{4,0,0, 0,7,0, 0,0,0},
{0,0,6, 0,0,0, 0,4,9}
};
int check_col(Grid grid, int x) {
uint8_t counters[10] = {0};
for (int i = 0; i < 9; i++) {
uint8_t v = grid[i][x];
if (v != 0 && counters[v] > 0)
return 0;
counters[v]++;
}
return 1;
}
int check_row(Grid grid, int y) {
uint8_t counters[10] = {0};
for (int i = 0; i < 9; i++) {
uint8_t v = grid[y][i];
if (v != 0 && counters[v] > 0)
return 0;
counters[v]++;
}
return 1;
}
int check_sqr(Grid grid, int x, int y) {
uint8_t counters[10] = {0};
y = (y % 3);
x = (x % 3);
for (int i = y*3; i < (y*3)+3; i++) {
for (int j = x*3; j < (x*3)+3; j++) {
uint8_t v = grid[i][j];
if (v != 0 && counters[v] > 0)
return 0;
counters[v]++;
}
}
return 1;
}
int guess(Grid grid, int x, int y) {
int next_x = (x+1);
int next_y = y;
if (next_x > 8) {
next_x = 0; next_y++;
}
if (grid[y][x] != 0) {
if (next_y > 8)
return 1;
return guess(grid, next_x, next_y);
} else {
for(int v = 1; v < 10; v++) {
grid[y][x] = v;
int r = check_row(grid, y);
int c = check_col(grid, x);
int s = check_sqr(grid, x, y);
if (c && r && s) {
if (next_y > 8)
return 1;
int r = guess(grid, next_x, next_y);
if (r) {
return 1;
}
}
}
grid[y][x] = 0;
}
return 0;
}
void solve(Grid grid) {
int r = guess(grid, 0, 0);
if (!r) {
printf("Could not solve it!\n");
}
}
const char* const digits = "0123456789";
void display_orig(Grid grid, Grid orig) {
puts("┌───────┬───────┬───────┐");
for(int i = 0; i < 9; i++) {
printf("│ ");
for(int j = 0; j < 9; j++) {
uint8_t v = grid[i][j];
if (orig && orig[i][j])
printf("\e[1m%c\e[0m ", digits[v]);
else
printf("%c ", v ? digits[v] : '.');
if ((j+1) % 3 == 0) printf("│ ");
}
putchar('\n');
if ( i != 8 && (i+1) % 3 == 0)
puts("├───────┼───────┼───────┤");
}
puts("└───────┴───────┴───────┘");
}
void display(Grid grid) {
display_orig(grid, 0);
}
Grid copy_grid(Grid g) {
Grid grid = (Grid)malloc(9*sizeof(uint8_t*));
for(int i = 0; i < 9; i++) {
grid[i] = (uint8_t*)malloc(9*sizeof(uint8_t));
}
for(int i = 0; i < 9; i++) {
memcpy(grid[i], g[i], 9*sizeof(uint8_t));
}
return grid;
}
Grid array_to_grid(const uint8_t g[9][9]) {
Grid grid = (Grid)malloc(9*sizeof(uint8_t*));
for(int i = 0; i < 9; i++) {
grid[i] = (uint8_t*)malloc(9*sizeof(uint8_t));
}
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
grid[i][j] = g[i][j];
}
}
return grid;
}
int main(int argc, char** argv) {
Grid g = array_to_grid(grid);
Grid orig = copy_grid(g);
display(g);
solve(g);
display_orig(g, orig);
return 0;
}