SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <Windows.h> | |
3 | #include <conio.h> | |
4 | ||
5 | using namespace std; | |
6 | ||
7 | char sudoku[9][9]; | |
8 | ||
9 | void printSudoku(bool); | |
10 | void recursiveScan(); | |
11 | void method1(bool &); | |
12 | void method2(bool &); | |
13 | bool checkBox(int, int, char); | |
14 | ||
15 | int main() | |
16 | { | |
17 | // initialize sudoku[][] | |
18 | for (int i = 0; i < 9; i++) | |
19 | { | |
20 | for (int j = 0; j < 9; j++) | |
21 | { | |
22 | sudoku[i][j] = ' '; | |
23 | } | |
24 | } | |
25 | // print empty grid | |
26 | printSudoku(false); | |
27 | // input sudoku | |
28 | printSudoku(true); | |
29 | system("pause"); | |
30 | system("CLS"); | |
31 | // solve sudoku | |
32 | recursiveScan(); | |
33 | system("pause"); | |
34 | return 0; | |
35 | } | |
36 | ||
37 | void printSudoku(bool input) | |
38 | { | |
39 | // reset cursor position | |
40 | COORD cursor = { 0, 0 }; | |
41 | SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursor); | |
42 | char key; | |
43 | for (int i = 0; i < 9; i++) | |
44 | { | |
45 | for (int j = 0; j < 9; j++) | |
46 | { | |
47 | switch (j) | |
48 | { | |
49 | case 0: | |
50 | { | |
51 | // print grid rows | |
52 | switch (i) | |
53 | { | |
54 | case 1: | |
55 | case 2: | |
56 | case 4: | |
57 | case 5: | |
58 | case 7: | |
59 | case 8: | |
60 | { | |
61 | cout << char(196) | |
62 | << char(197) | |
63 | << char(196) | |
64 | << char(197) | |
65 | << char(196) | |
66 | << char(186) | |
67 | << char(196) | |
68 | << char(197) | |
69 | << char(196) | |
70 | << char(197) | |
71 | << char(196) | |
72 | << char(186) | |
73 | << char(196) | |
74 | << char(197) | |
75 | << char(196) | |
76 | << char(197) | |
77 | << char(196) | |
78 | << '\n'; | |
79 | break; | |
80 | } | |
81 | case 3: | |
82 | case 6: | |
83 | { | |
84 | cout << char(205) | |
85 | << char(205) | |
86 | << char(205) | |
87 | << char(205) | |
88 | << char(205) | |
89 | << char(206) | |
90 | << char(205) | |
91 | << char(205) | |
92 | << char(205) | |
93 | << char(205) | |
94 | << char(205) | |
95 | << char(206) | |
96 | << char(205) | |
97 | << char(205) | |
98 | << char(205) | |
99 | << char(205) | |
100 | << char(205) | |
101 | << '\n'; | |
102 | break; | |
103 | } | |
104 | default: | |
105 | { | |
106 | break; | |
107 | } | |
108 | } | |
109 | break; | |
110 | } | |
111 | // print grid columns | |
112 | case 1: | |
113 | case 2: | |
114 | case 4: | |
115 | case 5: | |
116 | case 7: | |
117 | case 8: | |
118 | { | |
119 | cout << char(179); | |
120 | break; | |
121 | } | |
122 | case 3: | |
123 | case 6: | |
124 | { | |
125 | cout << char(186); | |
126 | break; | |
127 | } | |
128 | default: | |
129 | break; | |
130 | } | |
131 | // get initial sudoku from user | |
132 | if (input) | |
133 | { | |
134 | do | |
135 | { | |
136 | key = _getch(); | |
137 | } while (!strchr("123456789 ", key)); | |
138 | sudoku[i][j] = key; | |
139 | } | |
140 | cout << sudoku[i][j]; | |
141 | } | |
142 | cout << '\n'; | |
143 | } | |
144 | } | |
145 | ||
146 | // perform both methods while either method is still successful | |
147 | void recursiveScan() | |
148 | { | |
149 | bool meth1, meth2; | |
150 | do | |
151 | { | |
152 | printSudoku(false); | |
153 | method1(meth1); | |
154 | method2(meth2); | |
155 | Sleep(500); | |
156 | } while (meth1 || meth2); | |
157 | } | |
158 | ||
159 | // test if each box can only be a specific number | |
160 | void method1(bool &meth1) | |
161 | { | |
162 | meth1 = false; | |
163 | bool boxes[9]; | |
164 | for (int i = 0; i < 9; i++) | |
165 | { | |
166 | for (int j = 0; j < 9; j++) | |
167 | { | |
168 | // if current box is already solved, abort | |
169 | if (sudoku[i][j] != ' ') | |
170 | { | |
171 | continue; | |
172 | } | |
173 | // initialize boxes[] | |
174 | for (int k = 0; k < 9; k++) | |
175 | { | |
176 | boxes[k] = true; | |
177 | } | |
178 | // keep track of which numbers this box cannot be | |
179 | for (int ii = 0; ii < 9; ii++) | |
180 | { | |
181 | if (sudoku[ii][j] != ' ') | |
182 | { | |
183 | boxes[(sudoku[ii][j] - '0') - 1] = false; | |
184 | } | |
185 | } | |
186 | for (int jj = 0; jj < 9; jj++) | |
187 | { | |
188 | if (sudoku[i][jj] != ' ') | |
189 | { | |
190 | boxes[(sudoku[i][jj] - '0') - 1] = false; | |
191 | } | |
192 | } | |
193 | for (int ii = i - (i % 3); ii < i - (i % 3) + 3; ii++) | |
194 | { | |
195 | for (int jj = j - (j % 3); jj < j - (j % 3) + 3; jj++) | |
196 | { | |
197 | if (sudoku[ii][jj] != ' ') | |
198 | { | |
199 | boxes[(sudoku[ii][jj] - '0') - 1] = false; | |
200 | } | |
201 | } | |
202 | } | |
203 | // count how many numbers this box can be | |
204 | int options = 0; | |
205 | char answer; | |
206 | for (int k = 0; k < 9; k++) | |
207 | { | |
208 | if (boxes[k] == true) | |
209 | { | |
210 | options++; | |
211 | if (options > 1) | |
212 | { | |
213 | break; | |
214 | } | |
215 | answer = k + 1 + '0'; | |
216 | } | |
217 | } | |
218 | // if this box can only be one number, success | |
219 | if (options == 1) | |
220 | { | |
221 | sudoku[i][j] = answer; | |
222 | meth1 = true; | |
223 | } | |
224 | } | |
225 | } | |
226 | } | |
227 | ||
228 | // test if each row/column/3x3 box has only one space left for a specific number | |
229 | void method2(bool &meth2) | |
230 | { | |
231 | meth2 = false; | |
232 | int options; | |
233 | int answeri, answerj; | |
234 | for (char n = '1'; n <= '9'; n++) | |
235 | { | |
236 | for (int i = 0; i < 9; i++) | |
237 | { | |
238 | // keep track of how many boxes in this row can be n | |
239 | options = 0; | |
240 | for (int j = 0; j < 9; j++) | |
241 | { | |
242 | if (checkBox(i, j, n)) | |
243 | { | |
244 | options++; | |
245 | if (options > 1) | |
246 | { | |
247 | break; | |
248 | } | |
249 | answerj = j; | |
250 | } | |
251 | } | |
252 | // if only one box in this row can be n, success | |
253 | if (options == 1) | |
254 | { | |
255 | sudoku[i][answerj] = n; | |
256 | meth2 = true; | |
257 | } | |
258 | } | |
259 | for (int j = 0; j < 9; j++) | |
260 | { | |
261 | // keep track of how many boxes in this column can be n | |
262 | options = 0; | |
263 | for (int i = 0; i < 9; i++) | |
264 | { | |
265 | if (checkBox(i, j, n)) | |
266 | { | |
267 | options++; | |
268 | if (options > 1) | |
269 | { | |
270 | break; | |
271 | } | |
272 | answeri = i; | |
273 | } | |
274 | } | |
275 | // if only one box in this column can be n, success | |
276 | if (options == 1) | |
277 | { | |
278 | sudoku[answeri][j] = n; | |
279 | meth2 = true; | |
280 | } | |
281 | } | |
282 | for (int i = 0; i <= 6; i += 3) | |
283 | { | |
284 | for (int j = 0; j <= 6; j += 3) | |
285 | { | |
286 | // keep track of how many boxes in this 3x3 box can be n | |
287 | options = 0; | |
288 | for (int ii = i; ii < i + 3; ii++) | |
289 | { | |
290 | for (int jj = j; jj < j + 3; jj++) | |
291 | { | |
292 | if (checkBox(ii, jj, n)) | |
293 | { | |
294 | options++; | |
295 | if (options > 1) | |
296 | { | |
297 | break; | |
298 | } | |
299 | answeri = ii; | |
300 | answerj = jj; | |
301 | } | |
302 | } | |
303 | } | |
304 | // if only one box in this 3x3 box can be n, success | |
305 | if (options == 1) | |
306 | { | |
307 | sudoku[answeri][answerj] = n; | |
308 | meth2 = true; | |
309 | } | |
310 | } | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
315 | // test if sudoku[i][j] may be n | |
316 | bool checkBox(int i, int j, char n) | |
317 | { | |
318 | if (sudoku[i][j] != ' ') | |
319 | return false; | |
320 | // search this column for n | |
321 | for (int ii = 0; ii < 9; ii++) | |
322 | { | |
323 | if (sudoku[ii][j] == n) | |
324 | { | |
325 | return false; | |
326 | } | |
327 | } | |
328 | // search this row for n | |
329 | for (int jj = 0; jj < 9; jj++) | |
330 | { | |
331 | if (sudoku[i][jj] == n) | |
332 | { | |
333 | return false; | |
334 | } | |
335 | } | |
336 | // search this 3x3 box for n | |
337 | for (int ii = i - (i % 3); ii < i - (i % 3) + 3; ii++) | |
338 | { | |
339 | for (int jj = j - (j % 3); jj < j - (j % 3) + 3; jj++) | |
340 | { | |
341 | if (sudoku[ii][jj] == n) | |
342 | { | |
343 | return false; | |
344 | } | |
345 | } | |
346 | } | |
347 | // n was not found | |
348 | return true; | |
349 | } |