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
|
/*
* Sudoku: A plug-in for the Video Disk Recorder
*
* Copyright (C) 2005-2008, 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 "solver.h"
#include "backtrack.h"
#include "puzzle.h"
#include <assert.h>
using namespace Sudoku;
using namespace BackTrack;
//--- class Sudoku::Solver -----------------------------------------------------
/** Constructor */
Solver::Solver(Puzzle& puzzle, bool random_init, unsigned int max_iter) :
Algorithm(*this, max_iter), puzzle(puzzle), random_init(random_init)
{
free_count = 0;
for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
if (!puzzle.given(p))
++free_count;
}
/** Set the element to the first sibling. */
void Solver::set_first_at(unsigned int level)
{
assert(level < free_count);
const Pos p = puzzle.next_cell();
assert(p <= Pos::last());
free_list[level] = p;
puzzle.set(p, 0);
if (level == 0)
random = random_init;
unsigned int i, count = puzzle.numbers_count(p);
if (count != 0)
{
puzzle.set(p, puzzle.next_number(p));
if (random)
for (count = rand(count), i = 0; i < count; ++i)
puzzle.set(p, puzzle.next_number(p));
}
else
puzzle.set(p, 1);
}
/** Set the element to the next sibling. */
void Solver::set_next_at(unsigned int level)
{
assert(level < free_count);
Pos p = free_list[level];
unsigned int n = puzzle.next_number(p);
if (n != 0)
puzzle.set(p, n);
}
/** Reset the element. */
void Solver::reset_at(unsigned int level)
{
assert(level < free_count);
puzzle.set(free_list[level], 0);
random = false;
}
/** Check if the element is set to the last sibling. */
bool Solver::is_last_at(unsigned int level) const
{
assert(level < free_count);
return puzzle.next_number(free_list[level]) == 0;
}
/** Check if the element is valid (following elements ignored). */
bool Solver::is_valid_at(int level) const
{
assert(level < int(free_count));
if (level < 0)
return puzzle.solved();
return !puzzle.error(free_list[level]);
}
/** Check if the level is the last possible level. */
bool Solver::is_last_level(int level) const
{
return level >= int(free_count) - 1;
}
|