-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku.c
200 lines (177 loc) · 4.32 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAJOR 9
#define MINOR 3
#define EMPTY 0
#define NONE -1
#define BUFFER_SIZE 256
typedef int8_t sudoku_grid_t[9][9];
/*
** Find the next empty cell in the grid.
*/
static void next_empty(sudoku_grid_t sudoku, int *row, int *col)
{
for (int i = 0; i < MAJOR; ++i) {
for (int j = 0; j < MAJOR; ++j) {
if (sudoku[i][j] == EMPTY) {
*row = i;
*col = j;
return;
}
}
}
*row = NONE;
*col = NONE;
}
/*
** Check if a given value is legal at given coordinates.
** For a value to be legal, it has to be unique in its:
** - row
** - column
** - MINOR x MINOR partition
*/
static bool is_legal(sudoku_grid_t sudoku, int8_t guess, int row, int col)
{
// Check row
for (int i = 0; i < MAJOR; ++i) {
if (row == i)
continue;
if (sudoku[i][col] == guess)
return false;
}
// Check column
for (int j = 0; j < MAJOR; ++j) {
if (col == j)
continue;
if (sudoku[row][j] == guess)
return false;
}
// Check partition
for (int i = MINOR*(row/MINOR); i < MINOR*(row/MINOR + 1); ++i) {
for (int j = MINOR*(col/MINOR); j < MINOR*(col/MINOR + 1); ++j) {
if (row == i && col == j)
continue;
if (sudoku[i][j] == guess)
return false;
}
}
return true;
}
/*
** Solve the puzzle. The grid must be filled in before this function is called.
**
** Returns:
** - True if the puzzle was solved
** - False if it is impossible to solve it.
**
** The algorithm used here is very simple:
** - if there is a legal move to make, make it and recurse
** - if a legal move is not possible, backtrack
** - if backtracked out to the caller, the puzzle has no solution
*/
static bool solve_sudoku(sudoku_grid_t sudoku)
{
int row, col;
next_empty(sudoku, &row, &col);
// If there are no empty cells, we solved the puzzle
if (row == -1 && col == -1)
return true;
for (int8_t guess = 1; guess <= MAJOR; ++guess) {
if (is_legal(sudoku, guess, row, col)) {
sudoku[row][col] = guess;
if (solve_sudoku(sudoku))
return true;
}
}
// This cell could not be solved => zero it out
sudoku[row][col] = EMPTY;
return false;
}
/*
** Read sudoku from a given file.
**
** Empty cells must be represented as EMPTY or the character '.'
*/
static bool read_sudoku(sudoku_grid_t sudoku, FILE *file)
{
char buffer[BUFFER_SIZE];
// Read one line at a time
for (int i = 0; i < MAJOR; ++i) {
int j = 0;
// Plant a canary
buffer[0] = '\0';
// Read from file (the cast is needed to silence a warning)
fgets((char *__restrict)&buffer, BUFFER_SIZE, file);
// If the canary is still there, nothing was read from the file.
if (buffer[0] == '\0') {
fprintf(stderr, "Not enough lines of input: line %d missing\n", i + 1);
return false;
}
// Parse the line
for (int pos = 0; buffer[pos] != '\0'; ++pos) {
if (buffer[pos] >= '0' && buffer[pos] <= '9') {
sudoku[i][j] = buffer[pos] - '0';
++j;
// Also interpret dots as empty cells
} else if (buffer[pos] == '.') {
sudoku[i][j] = EMPTY;
++j;
}
// If there are too many values on the same linem
// bail out with an error (detected outside of the for-loop)
if (j > MAJOR) {
j = 0;
break;
}
}
// If there were not enough values (or too many), the input is invalid
if (j < MAJOR) {
fprintf(stderr, "Faulty input on line %d\n", i + 1);
return false;
}
}
return true;
}
/*
** Print the sudoku grid out as text.
*/
static void print_sudoku(sudoku_grid_t sudoku)
{
// Upper border
printf(" -------------------------\n");
for (int i = 0; i < MAJOR; ++i) {
// Horizontal partition separator
if ((i != 0) && (i % MINOR == 0))
printf(" --------+-------+--------\n");
for (int j = 0; j < MAJOR; ++j) {
// Left border and vertical partition separator
if (j % MINOR == 0)
printf(" |");
if (sudoku[i][j] == EMPTY)
// Empty cells are printed as dots
printf(" .");
else
// Everything else - as numbers
printf(" %d", sudoku[i][j]);
}
// Right border
printf(" |\n");
}
// Lower border
printf(" -------------------------\n");
}
int main(int argc, const char *argv[])
{
sudoku_grid_t sudoku;
if (argc != 2) {
fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
exit(EXIT_FAILURE);
}
FILE *file = fopen(argv[1], "rb");
if (!read_sudoku(sudoku, file))
exit(EXIT_FAILURE);
solve_sudoku(sudoku);
print_sudoku(sudoku);
exit(EXIT_SUCCESS);
}