-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_serial.c
401 lines (380 loc) · 13 KB
/
sudoku_serial.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# include "stdio.h"
# include "sys/time.h"
# include <string.h>
# include <stdlib.h>
# include <time.h>
/*
*
* map[81]: Store the values of 81 cells in the sudoku map
* ------------------------- ------------------------- -------------------------
* | map[0] map[1] map[2] | map[3] map[4] map[5] | map[6] map[7] map[8] |
* | map[9] map[10] map[11] | map[12] map[13] map[14] | map[15] map[16] map[17] |
* | map[18] map[19] map[20] | map[21] map[22] map[23] | map[24] map[25] map[26] |
* ------------------------- ------------------------- -------------------------
* | map[27] map[28] map[29] | map[30] map[31] map[32] | map[33] map[34] map[35] |
* | map[36] map[37] map[38] | map[39] map[40] map[41] | map[42] map[43] map[44] |
* | map[45] map[46] map[47] | map[48] map[49] map[50] | map[51] map[52] map[53] |
* ------------------------- ------------------------- -------------------------
* | map[54] map[55] map[56] | map[57] map[58] map[59] | map[60] map[61] map[62] |
* | map[63] map[64] map[65] | map[66] map[67] map[68] | map[69] map[70] map[71] |
* | map[72] map[73] map[74] | map[75] map[76] map[77] | map[78] map[79] map[80] |
* ------------------------- ------------------------- -------------------------
*
* (x, y): Coordinate of each cell in sudoku map
* ------------------- ------------------- -------------------
* | (1,1) (1,2) (1,3) | (1,4) (1,5) (1,6) | (1,7) (1,8) (1,9) |
* | (2,1) (2,2) (2,3) | (2,4) (2,5) (2,6) | (2,7) (2,8) (2,9) |
* | (3,1) (3,2) (3,3) | (3,4) (3,5) (3,6) | (3,7) (3,8) (3,9) |
* ------------------- ------------------- -------------------
* | (4,1) (4,2) (4,3) | (4,4) (4,5) (4,6) | (4,7) (4,8) (4,9) |
* | (5,1) (5,2) (5,3) | (5,4) (5,5) (5,6) | (5,7) (5,8) (5,9) |
* | (6,1) (6,2) (6,3) | (6,4) (6,5) (6,6) | (6,7) (6,8) (6,9) |
* ------------------- ------------------- -------------------
* | (7,1) (7,2) (7,3) | (7,4) (7,5) (7,6) | (7,7) (7,8) (7,9) |
* | (8,1) (8,2) (8,3) | (8,4) (8,5) (8,6) | (8,7) (8,8) (8,9) |
* | (9,1) (9,2) (9,3) | (9,4) (9,5) (9,6) | (9,7) (9,8) (9,9) |
* ------------------- ------------------- -------------------
*
* (i, j): Divide the sudoku map into 9 small boxes
* -------- -------- --------
* | (0, 0) | (0, 1) | (0, 2) |
* -------- -------- --------
* | (1, 0) | (1, 1) | (1, 2) |
* -------- -------- --------
* | (2, 0) | (2, 1) | (2, 2) |
* -------- -------- --------
*/
/*
* Function: SetCellValue
* --------------------
* Set value for the cell (x,y) in sudoku map
*
* x, y: coordinate of cell (x, y)
* value: Set the value of cell (x, y) as value
* map[]: sudoku map array
*/
void SetCellValue(int x, int y, char value, char map[])
{
int index = (x - 1) * 9 + y - 1;
map[index] = value;
}
/*
* Function: GetCellValue
* --------------------
* Get value from the cell (x,y) in sudoku map
*
* x, y: coordinate of cell (x, y)
* map[]: sudoku map array
*
* returns: the value of cell (x, y)
*/
char GetCellValue(int x, int y, char map[])
{
int index = (x - 1) * 9 + y - 1;
return map[index];
}
/*
* Function: RowColumnCheckDup
* --------------------
* Check whether there are duplicated values in row x or column y in sudoku map
*
* x, y: coordinate of cell (x, y)
* value: the value of cell (x, y)
* map[]: sudoku map array
*
* returns: return 0 if there are duplicated values in row x or column y in sudoku map, which means the sudoku is invalid. Otherwise, return 1
*/
int RowColumnCheckDup(int x, int y, char value, char map[])
{
int i = 0;
// If there are at least 2 cells in the column y with same value, return 0
for (i = 1; i <= 9; i++)
{
if (GetCellValue(i, y, map) == value)
{
if (x != i)
return 0;
}
}
// If there are at least 2 cells in the row x with same value, return 0
for (i = 1; i <= 9; i++)
{
if (GetCellValue(x, i, map) == value)
{
if (i != y)
return 0;
}
}
return 1;
}
/*
* Function: BoxCheckDup
* --------------------
* Check whether there are duplicated values in each small box (1 sudoku has 9 small boxes)
*
* x, y: coordinate of cell (x, y)
* value: the value of cell (x, y)
* map[]: sudoku map array
*
* returns: return 0 if there are duplicated values in the small box that (x, y) is located which means the sudoku is invalid. Otherwise, return 1
*/
int BoxCheckDup(int x, int y, int value, char map[])
{
int i = (x - 1) / 3;
int j = (y - 1) / 3;
for (int a = i * 3 + 1; a < i * 3 + 3 + 1; a++)
{
for (int b = j * 3 + 1; b < j * 3 + 3 + 1; b++)
{
if (GetCellValue(a, b, map) == value)
{
if (x != a && b != y)
return 0;
}
}
}
return 1;
}
/*
* Function: IsValid
* --------------------
* AND operation for the results of BoxCheckDup function and RowColumnCheckDup function
*
* x, y: coordinate of cell (x, y)
* value: the value of cell (x, y)
* map[]: sudoku map array
*
* returns: return 0 if there are duplicated values in the small box that (x, y) is located, or there are duplicated values in row x or column y in sudoku map, which means the sudoku is invalid. Otherwise, return 1.
*/
int IsValid(int x, int y, int value, char map[])
{
return BoxCheckDup(x, y, value, map) && RowColumnCheckDup(x, y, value, map);
}
/*
* Function: SudokuMapCheck
* --------------------
* Check the result of AND operation of the results of BoxCheckDup function and RowColumnCheckDup function, namely IsValid function above for each cell in sudoku map
*
* map[]: sudoku map array
*
* returns: return 0 if there are duplicated values in each small box, or there are duplicated values in each row or each column in sudoku map, which means the sudoku is invalid. Otherwise, return 1.
*/
int SudokuMapCheck(char map[])
{
int i, j;
i = 0;
j = 0;
for (i = 1; i < 10; i++)
{
for (j = 1; j < 10; j++)
{
int value = GetCellValue(i, j, map);
// In ParseArgv function, the cells without inputting values from the user will be filled in 0.
// In this function, only the cells with inputting values from the user need to be checked
if (value != 0)
{
if (!IsValid(i, j, value, map))
return 0;
}
}
}
return 1;
}
/*
* Function: SudokuPrint
* --------------------
* Print the solution of sudoku map with some format
*
* map[]: sudoku map array
*/
void SudokuPrint(char map[])
{
int i, j;
i = j = 0;
for (j = 1; j <= 9; j++)
{
printf("---");
if (j % 3 == 0)
{
printf("|");
}
}
printf("\n");
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 9; j++)
{
int value = GetCellValue(i, j, map);
// In ParseArgv function, the cells without inputting values from the user will be filled in 0, which should not be printed
if (value == 0)
printf(" ");
else
printf(" %d ", GetCellValue(i, j, map));
if (j % 3 == 0)
{
printf("|");
}
}
if (i % 3 == 0)
{
printf("\n");
for (j = 1; j <= 9; j++)
{
printf("---");
if (j % 3 == 0)
{
printf("|");
}
}
}
printf("\n");
}
printf("\n");
}
/*
* Function: SudokuSolution
* --------------------
* Figure out the possible solution for sudoku puzzle by travesing all the blank cells, trying to fill 1-9 in the blank cells one by one, and checking whether the numbers are reasonable.
*
* map[]: sudoku map array
*
* returns: return 0 if there is no reasonable solution to the sudoku puzzle, otherwise, return 1.
*/
int SudokuSolution(char map[])
{
int count = 0;
if (!SudokuMapCheck(map))
{
return count;
}
// Define a two-dimensional array with 81 rows (as there are 81 cells at maximum) and 2 columns, to record the coordinate of the cells without inputting values from the user in the two-dimensional array
int as[81][2] = {{0}};
// Define a variable, to record the number of cells which need to be filled in
int step = 0;
// Traverse all the cells in the sudoku map and store the corresponding info in the as[81][2] and step
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 10; j++)
{
if (GetCellValue(i, j, map) == 0)
{
as[step][0] = i;
as[step][1] = j;
step++;
}
}
}
//printf("The number of blank cells is %d!\n", step);
// Use "pos" to traverse all the blank cells and try to fill cur+1 or 0 in, cur range from 0 to 8
int pos = 0;
while (pos >= 0 && pos <= step)
{
int x = as[pos][0];
int y = as[pos][1];
int cur = GetCellValue(x, y, map);
if (cur < 9)
{
// Try to get a reasonable cur through IsValid function
while (IsValid(x, y, cur + 1, map) == 0)
{
cur++;
}
// If there is a possibly reasonable cur found, fill cur+1 in the current cell, and repeat same process for the next cell after pos++
if (cur < 9)
{
SetCellValue(x, y, cur + 1, map);
pos++;
}
// If there is no possibly reasonable cur found, fill 0 in the current cell and which also means the number filled in last cell was wrong, will try the next possibly reasonable value (namely the number + 1) in the last cell
else
{
SetCellValue(x, y, 0, map);
pos--;
}
}
// If the value in the current cell is 9, which means there is no possible values could be tried anymore, fill 0 in the current cell and which also means the number filled in last cell was wrong, will try the next possibly reasonable value (namely the number + 1) in the last cell
else
{
SetCellValue(x, y, 0, map);
pos--;
}
// When pos equals step, a valid solution for the sudoku puzzle is found successfully
if (pos == step)
{
count++;
// Print the full sudoku map after the calculation
SudokuPrint(map);
}
}
return count;
}
/*
* Function: ParseArgv
* --------------------
* Fill in the corresponding cells in sudoku map according to the coordinate and value inputted by the user when executing the program, for example: when the user executing ./sudoku_serial 1 1 2 1 4 6, means fill 2 into the cell (1, 1) and fill 6 into the cell (1, 4)
*
* argc: the number of the parameters when the user executes the program
* argv: the parameters when the user executes the program
* map[]: sudoku map array
*
* returns: return 0 if argc, argv (including both coordinates and values) or map[] is not valid, otherwise, fill the corresponding values into the cells in the sudoku map and return 1
*/
int ParseArgv(int argc, char **argv, char map[])
{
// Check if argc is valid
if ((argc - 1) % 3 != 0)
{
return 0;
}
// Check if map is valid
if (!map)
{
return 0;
}
// Initiate map with 0
memset(map, 0, sizeof(char) * 81);
// Check if argv is valid
for (int i = 1; i < argc; i += 3)
{
int x = atoi(argv[i]);
int y = atoi(argv[i + 1]);
char value = atoi(argv[i + 2]);
if (x < 1 || x > 9 || y < 1 || y > 9 || value < 1 || value > 9)
{
return 0;
}
// If argc, map and argv are valid, fill the corresponding values into the cells in the sudoku map
SetCellValue(x, y, value, map);
}
return 1;
}
/*
* Function: GetTime
* --------------------
* Get the current time
*
* returns: the current time
*/
long long GetTime()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
int main(int argc, char **argv)
{
char map[81];
// Exit if the user's input is not valid
if (!ParseArgv(argc, argv, map))
{
return 0;
}
// Print the sudoku map only with the user's input
//SudokuPrint(map);
// Calculate the number of the solutions to the sudoku based on the user's input and the time cost
long long start = GetTime();
int total_num_solutions = SudokuSolution(map);
long long end = GetTime();
printf("The num of solutions is %d, total time is %lld ms.\n", total_num_solutions, end - start);
return 0;
}