00001 /* 00002 * Sudoku: A plug-in for the Video Disk Recorder 00003 * 00004 * Copyright (C) 2005-2007, Thomas Günther <tom@toms-cafe.de> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00019 */ 00020 00021 #ifndef VDR_SUDOKU_SOLVER_H 00022 #define VDR_SUDOKU_SOLVER_H 00023 00024 #include "sudoku.h" 00025 #include "backtrack.h" 00026 #include "puzzle.h" 00027 00028 00029 namespace Sudoku 00030 { 00031 00032 //--- class Sudoku::Solver --------------------------------------------------- 00033 00034 /** Implementation of a backtracking algorithm to solve Sudoku puzzles 00035 * 00036 * The algorithm tries to set numbers in all free cells of the puzzle 00037 * sticking to the rules of Sudoku puzzles. 00038 * 00039 * There are three ways to use this class: 00040 * 00041 * 1. Solve a Sudoku puzzle with some numbers given: 00042 * 00043 * \code 00044 * Puzzle puzzle(36); // Generate a random puzzle with 36 givens. 00045 * Solver solver(puzzle); 00046 * solver.find_next_solution(); 00047 * if (solver.solution_is_valid()) 00048 * { 00049 * // Do something with the puzzle. 00050 * ... 00051 * } 00052 * solver.find_next_solution(); 00053 * bool more_than_one_solution = solver.solution_is_valid(); 00054 * \endcode 00055 * 00056 * 2. Search a random Sudoku solution: 00057 * 00058 * \code 00059 * Puzzle puzzle; // Generate an empty puzzle without givens. 00060 * Solver solver(puzzle, true); 00061 * solver.find_next_solution(); 00062 * if (solver.solution_is_valid()) 00063 * { 00064 * // Do something with the puzzle. 00065 * ... 00066 * } 00067 * \endcode 00068 * 00069 * 3. Search all Sudoku solutions: 00070 * 00071 * \code 00072 * Puzzle puzzle; // Generate an empty puzzle without givens. 00073 * Solver solver(puzzle); 00074 * solver.find_next_solution(); 00075 * while (solver.solution_is_valid()) 00076 * { 00077 * // Do something with the puzzle. 00078 * ... 00079 * solver.find_next_solution(); 00080 * } 00081 * \endcode 00082 */ 00083 class Solver : public BackTrack::Algorithm, public BackTrack::Solution 00084 { 00085 Puzzle& puzzle; 00086 Pos free_list[SDIM]; 00087 unsigned int free_count; 00088 bool random_init, random; 00089 00090 public: 00091 00092 /** Constructor */ 00093 Solver(Puzzle& puzzle, bool random_init = false, unsigned int max_iter = 0); 00094 00095 /** Set the element to the first sibling. */ 00096 virtual void set_first_at(unsigned int level); 00097 00098 /** Set the element to the next sibling. */ 00099 virtual void set_next_at(unsigned int level); 00100 00101 /** Reset the element. */ 00102 virtual void reset_at(unsigned int level); 00103 00104 /** Check if the element is set to the last sibling. */ 00105 virtual bool is_last_at(unsigned int level) const; 00106 00107 /** Check if the element is valid (following elements ignored). */ 00108 virtual bool is_valid_at(int level) const; 00109 00110 /** Check if the level is the last possible level. */ 00111 virtual bool is_last_level(int level) const; 00112 }; 00113 00114 } // namespace Sudoku 00115 00116 #endif // VDR_SUDOKU_SOLVER_H