16 for (y = 0; y < 9; y++)
17 for (x = 0; x < 9; x++)
28 static int check_validity(const int col, const int row)
30 if (col < 0 || col >= 9) {
31 DPRINT << "Invalid column: " << col << std::endl;
35 if (row < 0 || row >= 9) {
36 DPRINT << "Invalid row: " << row << std::endl;
42 char sudoku::get(const int col, const int row)
44 if (!check_validity(row, col)) {
45 DPRINT << "over here\n";
49 return table[col][row];
52 int sudoku::set(const int col, const int row, const char val)
56 if (!check_validity(col, row)) {
57 DPRINT << "over here\n";
61 if (table[col][row] != EMPTY) {
62 DPRINT << "Assigning value to a non-empty slot\n";
66 str = get_col_contents(col);
67 if (str.find(val, 0) != std::string::npos) {
68 DPRINT << "Illegal assignment " << col << "." << row << ": "
73 str = get_row_contents(row);
74 if (str.find(val, 0) != std::string::npos) {
75 DPRINT << "Illegal assignment " << col << "." << row << ": "
80 str = get_block_contents(col, row);
81 if (str.find(val, 0) != std::string::npos) {
82 DPRINT << "Illegal assignment " << col << "." << row << ": "
87 table[col][row] = val;
92 int sudoku::str_to_row(const int row, const std::string &str)
94 unsigned int i, col = 0;
97 if (row < 0 || row >= 9) {
98 DPRINT << "Illegal row " << row << std::endl;
102 for (i = 0; i < str.length(); i++) {
112 /* Ignore non-numbers */
113 if (chr < '1' || chr > '9')
116 set(col++, row, chr);
123 void sudoku::print(void)
127 printf("+-------+------+--------+\n");
128 for (y = 0; y < 9; y++) {
130 for (x = 0; x < 9; x++) {
131 printf("%c ", table[x][y]);
137 printf("+-------+-------+-------+\n");
141 void sudoku::clone_to(sudoku &to)
146 for (x = 0; x < 9; x++)
147 for (y = 0; y < 9; y++)
148 to.table[x][y] = table[x][y];
151 std::string sudoku::get_row_contents(int row)
156 for (col = 0; col < 9; col++) {
157 if (table[col][row] != EMPTY)
158 s += table[col][row];
164 std::string sudoku::get_col_contents(int col)
169 for (row = 0; row < 9; row++) {
170 if (table[col][row] != EMPTY)
171 s += table[col][row];
177 std::string sudoku::get_block_contents(int col, int row)
183 if (col < 0 || col >= 9)
184 DPRINT "Aiee " << col << std::endl;
185 if (row < 0 || row >= 9)
186 DPRINT "Aiee " << row << std::endl;
188 col = (col /= 3) * 3;
189 row = (row /= 3) * 3;
191 for (y = 0; y < 3; y++)
192 for (x = 0; x < 3; x++) {
193 c = get(x + col, y + row);
201 static std::string exclude_common(std::string s1, std::string s2)
203 std::string::iterator a, b;
206 for (a = s1.begin(); a != s1.end(); a++) {
207 for (b = s2.begin(); b != s2.end(); b++) {
218 std::string sudoku::get_legal_numbers(int col, int row)
221 std::string a, b, c, ret;
223 if (!check_validity(col, row)) {
224 DPRINT << "over here\n";
233 // std::cout << "[" << col << "." << row << "] ";
235 a = get_col_contents(col);
236 // std::cout << " col " << a;
238 b = get_row_contents(row);
239 // std::cout << " row " << b;
241 c = get_block_contents(col, row);
242 // std::cout << " block " << c;
244 ret = exclude_common("123456789", a);
245 ret = exclude_common(ret, b);
246 ret = exclude_common(ret, c);
248 // std::cout << " legal " << ret << std::endl;
252 int sudoku::fill_missing()
254 int x, y, solved = 0;
257 for (y = 0; y < 9; y++)
258 for (x = 0; x < 9; x++) {
259 num = get_legal_numbers(x, y);
260 if ((num != "0") && (num.length() == 1)) {
266 //std::cout << "Solved " << solved << " numbers\n";
272 int sudoku::get_best_guess(int &col, int &row)
275 int min = 0, filled = 0, illegals = 0;
279 * Initialize row and col with illegal values to spot possible
285 for (y = 0; (y < 9) && !illegals; y++)
286 for (x = 0; x < 9; x++) {
287 numbers = get_legal_numbers(x, y);
289 if (numbers == "0") {
294 if (numbers.length() == 0) {
300 min = numbers.length();
306 if ((int)numbers.length() < min) {
307 min = numbers.length();
313 // std::cout << x << "." << y << ": " << " min "
314 // << numbers << col << "." << row << std::endl;
317 if ((!min && !filled) || illegals) {
318 // DPRINT << "Is this sudoku has no solutions?\n";
322 if (!min && (filled == 99)) {
323 DPRINT << "This sudoku is solved\n";
330 std::vector<sudoku> sudoku::solve_all()
334 std::vector<sudoku> sudokus;
336 // Fill in as much as possible
339 std::cout << std::endl;
342 } while (fill_missing() != 0);
344 ret = get_best_guess(x, y);
346 // Did we solve it yet?
351 std::cout << "This is unsolvable, recursion: "
352 << recursion_depth << std::endl;
358 std::cout << "Solution found , recursion: "
359 << recursion_depth << std::endl;
361 sudokus.push_back(*this);
368 std::cout << "Not solved yet, guessing at depth "
369 << recursion_depth << std::endl;
372 // Try guessim something
373 str = get_legal_numbers(x, y);
374 for (i = 0; i < (int)str.length(); i++) {
375 std::vector<sudoku> results;
377 sudoku guess = *this;
381 std::cout << "Guessing number " << str[i] << " to "
382 << x << "." << y << std::endl;
385 guess.set(x, y, str[i]);
386 guess.recursion_depth = recursion_depth + 1;
389 results = guess.solve_all();
390 guesses += guess.guesses;
393 if (!results.empty()) {
394 /* printf("Got %d solutions, guessed %d: %s\n",
395 (int)results.size(), (int)str.size(),
398 for(j = 0;j < (int)results.size(); j++) {
399 sudokus.push_back(results.at(j));
400 guesses += results.at(j).guesses;
406 std::cout << "Guesses: " << guesses << std::endl;
408 // Return what was solved
412 sudoku sudoku::fill_with_random(int &solvable)
418 // Fill in as much as possible
421 //std::cout << std::endl;
424 } while (fill_missing() != 0);
426 ret = get_best_guess(x, y);
428 // Did we fill it yet?
434 //std::cout << "This is unsolvable, recursion: "
435 // << recursion_depth << std::endl;
442 //std::cout << "Solution found , recursion: "
443 // << recursion_depth << std::endl;
449 //std::cout << "Not solved yet, guessing at depth "
450 // << recursion_depth << std::endl;
453 // Try filling with something
454 str = get_legal_numbers(x, y);
455 while (str.length()) {
456 i = get_random() % str.length();
458 sudoku guess = *this;
459 guess.set(x, y, str[i]);
462 //std::cout << "Trying number " << str[i] << " to "
463 // << x << "." << y << std::endl;
466 guess.recursion_depth = recursion_depth + 1;
469 guess = guess.fill_with_random(can_solve);
485 int sudoku::used_numbers()
488 for (x = 0; x < 9; x++)
489 for (y = 0; y < 9; y++)
490 if (table[x][y] != EMPTY)
495 sudoku sudoku::remove_randomly(int min_guesses, int depth)
497 int x, y, i, count = used_numbers();
498 sudoku tmp = *this, tmp2;
499 std::vector<sudoku> results;
501 for (i = 0; i < count; i++) {
502 /* remove a number */
504 x = get_random() % 9;
505 y = get_random() % 9;
506 } while(tmp.get(x,y) == EMPTY);
507 tmp.table[x][y] = EMPTY;
509 //printf("Got now:\n");
514 results = tmp.solve_all();
516 /* Does it still have only one solution */
517 if (results.size() == 1) {
518 /* but not hard enough yet? */
519 if (tmp.guesses >= min_guesses) {
521 printf("%d, Good!\n", depth);
524 printf("%2d, got only %3d guesses\n", depth,
529 tmp2 = tmp.remove_randomly(min_guesses, depth + 1);
531 /* test for emptiness */
532 if (tmp2.guesses == 0) {
536 if (tmp2.guesses >= min_guesses) {
540 printf("%d: Guesses: %d, not good enough\n", depth,
545 if (results.size() > 1) {
546 /* oops, removed too many */
548 printf("%d, Got multiple results\n", depth);
552 if (results.empty()) {
553 fprintf(stderr, "%d, Fail. This can't happen?\n",
561 int sudoku::generate(int min_guesses)
564 sudoku tmp = fill_with_random(solvable);
569 /* Right, we got it filled */
570 tmp = tmp.remove_randomly(min_guesses, 0);