-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.c
124 lines (114 loc) · 2.59 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
/*
sudoku.c - Quick & Really Dirty Sudoku Solver - Version 0.9-beta
Copyright (C) 2005 Guillem Cantallops Ramis <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main (int argc, char **argv) {
char x, y, z, xx, yy, zz, xxx, yyy, zzz;
char board [9] [9];
char possi [10];
char changed, zeros;
if (argc != 2) {
return -1;
}
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++) {
board [x] [y] = argv [1] [x * 9 + y] - '0';
if ( (board [x] [y] < 0) || (board [x] [y] > 9) ) {
return -1;
}
}
}
if (argv [1] [9*9]) {
return -1;
}
do {
changed = 0;
zeros = 0;
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++) {
if (board [x] [y]) {
continue;
}
memset (possi, 0, sizeof (possi));
for (xx = 0; xx < 9; xx++) {
if (board [xx] [y] > 0 ) {
possi [board [xx] [y]] = 1;
}
}
for (yy = 0; yy < 9; yy++) {
if (board [x] [yy] > 0) {
possi [board [x] [yy]] = 1;
}
}
for (xx = x - (x % 3); xx < x - (x % 3) + 3; xx++) {
for (yy = y - (y % 3); yy < y - (y % 3) + 3; yy++) {
if (board [xx] [yy] > 0) {
possi [board [xx] [yy]] = 1;
}
}
}
for (z = 1; z <= 9; z++) {
if (possi [z]) {
possi [0] ++;
}
else {
zz = z;
}
}
switch (9 - possi [0]) {
case 0:
return -1;
break;
case 1:
board [x] [y] = zz;
changed = 1;
break;
default:
xxx = x;
yyy = y;
zeros ++;
break;
}
}
}
if (!changed) {
for (zzz = 1; zzz <= 9; zzz++) {
if (! possi [zzz]) {
if (! fork ()) {
board [xxx] [yyy] = zzz;
changed = 1;
break;
}
}
}
if (!changed) {
return -1;
}
}
}
while (zeros);
printf ("\n");
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++) {
printf ("%3d", board [x] [y]);
}
printf ("\n");
}
printf ("\n");
return 0;
}