author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:49:11 +0200 | |
changeset 32 | 9b56360ec64e |
parent 29 | 7abf6146898e |
child 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
1 |
/** |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
2 |
* test/demos/sudoku.c |
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 |
||
191 |
while ((i = getopt (argc, argv, "ch")) != -1) { |
|
192 |
switch (i) { |
|
193 |
case 'h': |
|
194 |
usage (argv[0]); |
|
195 |
return EXIT_SUCCESS; |
|
196 |
case 'c': |
|
197 |
printf ("%s%s", BGCOLOR, FGCOLOR); |
|
198 |
colored = 1; |
|
199 |
break; |
|
200 |
default: |
|
201 |
usage (argv[0]); |
|
202 |
return EXIT_FAILURE; |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
if ((DIM % 3) != 0) { |
|
207 |
fprintf (stderr, "DIM must be a multiple of 3\n"); |
|
208 |
fprintf (stderr, "RECOMPILE WITH -DDIM=9\n"); |
|
209 |
return EXIT_FAILURE; |
|
210 |
} |
|
211 |
||
212 |
if ((b = calloc (DIM, sizeof (Field *))) == NULL) { |
|
213 |
perror ("calloc"); |
|
214 |
return EXIT_FAILURE; |
|
215 |
} |
|
216 |
||
217 |
for (i = 0; i < DIM; i++) { |
|
218 |
if ((b[i] = malloc ((DIM * sizeof (Field)) + 1)) == NULL) { |
|
219 |
perror ("malloc"); |
|
220 |
return EXIT_FAILURE; |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
printf ("Sudoku - Enter a %dx%d Matrix\n", DIM, DIM); |
|
225 |
||
226 |
if (readBoard (b) == FALSE) { |
|
227 |
fprintf (stderr, "Input Error: Aborting\n"); |
|
228 |
return EXIT_FAILURE; |
|
229 |
} |
|
230 |
||
231 |
showBoard (b, colored); |
|
232 |
||
233 |
if ((ret = solveBoard (b, 0)) == TRUE) |
|
234 |
printf ("\nPuzzle solved after %lu recursions\n", iterations); |
|
235 |
else |
|
236 |
printf ("\nCannot solve your puzzle after %lu recursions\n", iterations); |
|
237 |
||
238 |
showBoard (b, colored); |
|
239 |
freeBoard (b); |
|
240 |
||
241 |
if (b != NULL) |
|
242 |
free (b); |
|
243 |
||
244 |
if (colored) |
|
245 |
printf ("%s", RESET); |
|
246 |
||
247 |
return EXIT_SUCCESS; |
|
248 |
} |