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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/*
* Sudoku: A plug-in for the Video Disk Recorder
*
* Copyright (C) 2005-2007, Thomas Günther <tom@toms-cafe.de>
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define USE_RAND
#include "generator.h"
#include "solver.h"
#include "backtrack.h"
#include "puzzle.h"
#include <assert.h>
using namespace Sudoku;
using namespace BackTrack;
//--- class Sudoku::Generator --------------------------------------------------
/** Constructor */
Generator::Generator(Puzzle& puzzle, unsigned int givens_count,
bool symmetric, unsigned int max_iter) :
Algorithm(*this, max_iter), puzzle(puzzle), symmetric(symmetric)
{
assert(givens_count <= SDIM);
// Search a random Sudoku solution.
for (bool found = false; !found;)
{
sudoku.reset();
Solver solver(sudoku, true);
solver.find_next_solution();
found = solver.solution_is_valid();
}
// If symmetric pos_list contains only the first halve of all positions.
pos_count = SDIM;
free_count = SDIM - givens_count;
free_center = symmetric && pos_count % 2 != 0 && free_count % 2 != 0;
if (symmetric)
pos_count /= 2, free_count /= 2;
// Fill pos_list with positions in random order.
bool list[pos_count];
unsigned int p, i, c;
for (p = 0; p < pos_count; ++p)
list[p] = true;
for (i = 0; i < pos_count; ++i)
{
c = rand(pos_count - i) + 1;
for (p = 0; p < pos_count; ++p)
if (list[p])
if (--c == 0)
break;
assert(p < pos_count);
list[p] = false;
pos_list[i] = p;
}
}
/** Set the element to the first sibling. */
void Generator::set_first_at(unsigned int level)
{
assert(level < free_count);
free_list[level] = 0;
}
/** Set the element to the next sibling. */
void Generator::set_next_at(unsigned int level)
{
assert(level < free_count);
++free_list[level];
}
/** Reset the element. */
void Generator::reset_at(unsigned int level)
{
}
/** Check if the element is set to the last sibling. */
bool Generator::is_last_at(unsigned int level) const
{
assert(level < free_count);
return free_list[level] >= pos_count - 1;
}
/** Check if the element is valid (following elements ignored). */
bool Generator::is_valid_at(int level) const
{
assert(level < int(free_count));
// free_list contains ordered indices to pos_list.
if (level > 0 && free_list[level] <= free_list[level - 1])
return false;
if (level >= 0 && free_list[level] > pos_count - (free_count - level))
return false;
// Fill list with marks for givens.
bool given_marks[SDIM];
int i;
for (i = 0; i < SDIM; ++i)
given_marks[i] = true;
for (i = 0; i <= level; ++i)
{
Pos p = pos_list[free_list[i]];
given_marks[p] = false;
if (symmetric)
given_marks[p.symmetric()] = false;
}
if (free_center)
given_marks[Pos::center()] = false;
// Set givens in puzzle and check if it has only one solution.
puzzle.set_givens(sudoku, given_marks);
Solver solver(puzzle);
solver.find_next_solution();
assert(solver.solution_is_valid());
solver.find_next_solution();
return !solver.solution_is_valid();
}
/** Check if the level is the last possible level. */
bool Generator::is_last_level(int level) const
{
return level >= int(free_count) - 1;
}
|