author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 05 Mar 2010 22:03:25 +0100 | |
changeset 113 | 397270b5d21a |
parent 85 | 9568a180fc43 |
child 133 | f46f9606dbfb |
permissions | -rw-r--r-- |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
1 |
/** |
77 | 2 |
* sudoku.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
* |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
5 |
* Sudoku - The Game |
0 | 6 |
*/ |
7 |
||
8 |
#include <stdio.h> |
|
9 |
#include <stdlib.h> |
|
10 |
#include <getopt.h> |
|
11 |
#include <string.h> |
|
12 |
#include <errno.h> |
|
13 |
#include <limits.h> |
|
14 |
||
15 |
#ifndef DIM |
|
16 |
#define DIM 9 |
|
17 |
#endif |
|
18 |
||
19 |
#define COLORED "\e[31m" |
|
20 |
#define FGCOLOR "\e[37m" |
|
21 |
#define BGCOLOR "\e[40m\e[2J" |
|
22 |
#define RESET "\e[0m" |
|
23 |
||
24 |
#define TRUE 1 |
|
25 |
#define FALSE 0 |
|
26 |
||
27 |
#ifndef u_int |
|
28 |
typedef unsigned int u_int; |
|
29 |
#endif |
|
30 |
||
31 |
#ifndef MAX_ITERATIONS |
|
32 |
#define MAX_ITERATIONS 10000000 |
|
33 |
#endif |
|
34 |
||
35 |
struct Field { |
|
36 |
u_int value; |
|
37 |
int isConst; |
|
38 |
}; |
|
39 |
||
40 |
/* the easiest way to get statistics */ |
|
41 |
static unsigned long iterations = 0; |
|
42 |
||
43 |
typedef struct Field Field; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
44 |
|
0 | 45 |
typedef Field **Board; |
46 |
||
47 |
/** |
|
48 |
* read nxn fields from commandline and store it in the Board variable |
|
49 |
*/ |
|
50 |
int readBoard (Board board) |
|
51 |
{ |
|
52 |
int i, j; |
|
53 |
||
54 |
u_int c; |
|
55 |
||
56 |
for (i = 0; i < DIM; i++) { |
|
57 |
for (j = 0; j < DIM; j++) { |
|
58 |
if (scanf ("%u", &c) < 1) { |
|
59 |
fprintf (stderr, "readBoard: Not enough elements\n"); |
|
60 |
fprintf (stderr, "readBoard: Got %d, needed %d\n", i * DIM + j, (DIM) * (DIM)); |
|
61 |
return FALSE; |
|
62 |
} |
|
63 |
board[i][j].value = c; |
|
64 |
board[i][j].isConst = (c == 0) ? 0 : 1; |
|
65 |
} |
|
66 |
} |
|
67 |
return TRUE; |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Show the content of the Board in a human readable manner |
|
72 |
*/ |
|
73 |
void showBoard (Board board, int colored) |
|
74 |
{ |
|
75 |
int i, j; |
|
76 |
||
77 |
for (i = 0; i < DIM; i++) { |
|
78 |
if ((i % 3) == 0) |
|
79 |
printf ("\n"); |
|
80 |
||
81 |
for (j = 0; j < DIM; j++) { |
|
82 |
if ((board[i][j].isConst && colored)) |
|
83 |
printf ("%s%2u%s ", COLORED, board[i][j].value, FGCOLOR); |
|
84 |
else |
|
85 |
printf ("%2u ", board[i][j].value); |
|
86 |
if (((j + 1) % 3) == 0) |
|
87 |
printf (" "); |
|
88 |
} |
|
89 |
printf ("\n"); |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* checks for the possibility of value at board[row][col] |
|
95 |
*/ |
|
96 |
int check (Board board, int row, int col, u_int value) |
|
97 |
{ |
|
98 |
int i, j; |
|
99 |
||
100 |
if (board[row][col].value > 0) |
|
101 |
return FALSE; |
|
102 |
||
103 |
for (i = 0; i < DIM; i++) { |
|
104 |
/* |
|
105 |
* check vertically |
|
106 |
*/ |
|
107 |
if (board[row][i].value == value) |
|
108 |
return FALSE; |
|
109 |
/* |
|
110 |
* check horizontally |
|
111 |
*/ |
|
112 |
if (board[i][col].value == value) |
|
113 |
return FALSE; |
|
114 |
} |
|
115 |
||
116 |
/* |
|
117 |
* check block for unique values |
|
118 |
*/ |
|
119 |
for (i = row - (row % 3); i < row - (row % 3) + 3; i++) |
|
120 |
for (j = col - (col % 3); j < col - (col % 3) + 3; j++) |
|
121 |
if (board[i][j].value == value) |
|
122 |
return FALSE; |
|
123 |
||
124 |
return TRUE; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* go recursive from k to DIM^2 and try to solve the puzzle |
|
129 |
*/ |
|
130 |
int solveBoard (Board board, int k) |
|
131 |
{ |
|
132 |
u_int i; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
133 |
|
0 | 134 |
int q; |
135 |
||
136 |
if ((++iterations > MAX_ITERATIONS)) |
|
137 |
return FALSE; |
|
138 |
||
139 |
while (k < (DIM) * (DIM) && (board[k / DIM][k % DIM].value != 0)) |
|
140 |
k++; |
|
141 |
||
142 |
if (k == (DIM) * (DIM)) |
|
143 |
return TRUE; |
|
144 |
||
145 |
for (q = FALSE, i = 1; q == FALSE && i <= 9; i++) { |
|
146 |
if (check (board, k / DIM, k % DIM, i) == TRUE) { |
|
147 |
board[k / DIM][k % DIM].value = i; |
|
148 |
if (((DIM) * (DIM) - 1) == k) |
|
149 |
q = TRUE; |
|
150 |
else if ((q = solveBoard (board, k + 1)) == FALSE) { |
|
151 |
board[k / DIM][k % DIM].value = 0; |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
return q; |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Deletes every Field of the Board (CLEANUP) |
|
161 |
*/ |
|
162 |
void freeBoard (const Board board) |
|
163 |
{ |
|
164 |
int i; |
|
165 |
||
166 |
for (i = 0; i < DIM; i++) { |
|
167 |
if (board[i] != NULL) |
|
168 |
free (board[i]); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Show plain usage screen |
|
174 |
*/ |
|
175 |
void usage (char *name) |
|
176 |
{ |
|
177 |
printf ("Usage: %s [-c] [-h]\n\n", name); |
|
178 |
printf ("-c\t\tcolored output\n"); |
|
179 |
printf ("-h\t\tshows this help\n"); |
|
180 |
printf ("Report bugs to mbroeker@largo.homelinux.org\n"); |
|
181 |
} |
|
182 |
||
183 |
int main (int argc, char **argv) |
|
184 |
{ |
|
185 |
Board b; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
186 |
|
0 | 187 |
int i; |
188 |
int ret; |
|
189 |
int colored = 0; |
|
190 |
||
85
9568a180fc43
[getopt]: support for long options
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
191 |
struct option options[] = { |
9568a180fc43
[getopt]: support for long options
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
192 |
{"colored", 0, 0, 'c'}, |
9568a180fc43
[getopt]: support for long options
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
193 |
{"help", 0, 0, 'h'}, |
9568a180fc43
[getopt]: support for long options
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
194 |
}; |
9568a180fc43
[getopt]: support for long options
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
195 |
|
9568a180fc43
[getopt]: support for long options
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
196 |
while ((i = getopt_long_only (argc, argv, "ch", options, NULL)) != -1) { |
0 | 197 |
switch (i) { |
198 |
case 'h': |
|
199 |
usage (argv[0]); |
|
200 |
return EXIT_SUCCESS; |
|
201 |
case 'c': |
|
202 |
printf ("%s%s", BGCOLOR, FGCOLOR); |
|
203 |
colored = 1; |
|
204 |
break; |
|
205 |
default: |
|
206 |
usage (argv[0]); |
|
207 |
return EXIT_FAILURE; |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
if ((DIM % 3) != 0) { |
|
212 |
fprintf (stderr, "DIM must be a multiple of 3\n"); |
|
213 |
fprintf (stderr, "RECOMPILE WITH -DDIM=9\n"); |
|
214 |
return EXIT_FAILURE; |
|
215 |
} |
|
216 |
||
217 |
if ((b = calloc (DIM, sizeof (Field *))) == NULL) { |
|
218 |
perror ("calloc"); |
|
219 |
return EXIT_FAILURE; |
|
220 |
} |
|
221 |
||
222 |
for (i = 0; i < DIM; i++) { |
|
223 |
if ((b[i] = malloc ((DIM * sizeof (Field)) + 1)) == NULL) { |
|
224 |
perror ("malloc"); |
|
225 |
return EXIT_FAILURE; |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
printf ("Sudoku - Enter a %dx%d Matrix\n", DIM, DIM); |
|
230 |
||
231 |
if (readBoard (b) == FALSE) { |
|
232 |
fprintf (stderr, "Input Error: Aborting\n"); |
|
233 |
return EXIT_FAILURE; |
|
234 |
} |
|
235 |
||
236 |
showBoard (b, colored); |
|
237 |
||
238 |
if ((ret = solveBoard (b, 0)) == TRUE) |
|
239 |
printf ("\nPuzzle solved after %lu recursions\n", iterations); |
|
240 |
else |
|
241 |
printf ("\nCannot solve your puzzle after %lu recursions\n", iterations); |
|
242 |
||
243 |
showBoard (b, colored); |
|
244 |
freeBoard (b); |
|
245 |
||
246 |
if (b != NULL) |
|
247 |
free (b); |
|
248 |
||
249 |
if (colored) |
|
250 |
printf ("%s", RESET); |
|
251 |
||
252 |
return EXIT_SUCCESS; |
|
253 |
} |