talen bedoel je?quote:
| 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 | // Include the class include 'StateSolver.class.php'; // This is what a given sudoku should look like (solution on the right) $sudoku = array( 0,2,0,4,0,9,5,0,6, // 321 489 576 0,4,5,0,7,0,8,0,9, // 645 173 829 0,7,0,0,2,6,0,0,0, // 879 526 413 0,0,0,0,3,0,6,0,1, // 587 234 691 9,0,2,0,6,0,0,0,0, // 912 865 347 0,0,0,7,0,1,0,5,0, // 436 791 258 0,0,0,3,1,0,0,0,4, // 258 317 964 7,0,0,0,0,0,1,0,0, // 794 658 132 0,0,3,9,0,2,7,8,0); // 163 942 785 // Instantiate the solver class $stateSolver = new StateSolver($sudoku); // Display the current sudoku DisplaySudoku($sudoku); // Obtain start time $beginTime = microtime(); // Call the solver $stateSolver->findSolution(); // Obtain end time $endTime = microtime(); // Calculate the total time $totalTime = $endTime - $beginTime; echo 'Elapsed time: '.$totalTime.'ms<br />'; // Display the solved sudoku DisplaySudoku($stateSolver->sudoku); function displaySudoku($sudoku) { for ($i = 0; $i < 81; $i++) { echo ' ' . $sudoku[$i] . ' '; if (($i + 1) % 9 == 0) { echo '<br />'; } } } ?> |
| 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | /* Sudoku State Solver v0.1 [04-feb-2006] -------------------------------------- All code by Mark Jelsma */ class StateSolver { ///////////////////////// /// PUBLIC ATTRIBUTES /// ///////////////////////// // Holds the sudoku puzzel as an array public $sudoku; ////////////////////////// /// PRIVATE ATTRIBUTES /// ////////////////////////// // Holds the 27 candidatelists as an array private $_candidates; // Holds the list of empty cells as an array private $_emptyCells; // Determines whether or not the algorithm has found a solution private $_ready; //////////////////// /// CONSTRUCTORS /// //////////////////// public function __construct($sudoku) { $this->sudoku = $sudoku; } ////////////////////// /// PUBLIC METHODS /// ////////////////////// // Initialize the solving algorithm public function findSolution() { $column = 0; $row = 0; $region = 0; $eIndex = 0; // Fill the candidatelists with all 9 bits set for ($i = 0; $i < 27; $i++) { $this->_candidates[$i] = 511; } // Exclude invalid candidates and get empty cells for ($i = 0; $i < 81; $i++) { if ($this->sudoku[$i] == 0) { // Add this empty cell to the list $this->_emptyCells[$eIndex++] = $i; } else { // Exclude this number from the candidatelists $this->_getCandidateLists($i, $column, $row, $region); $this->_exclude($this->_candidates[$column], $this->sudoku[$i]); $this->_exclude($this->_candidates[$row], $this->sudoku[$i]); $this->_exclude($this->_candidates[$region], $this->sudoku[$i]); } } // Set the ready flag to false $this->_ready = false; // Run the recursive backtracking algorithm $this->_solve(0); } /////////////////////// /// PRIVATE METHODS /// /////////////////////// // Recursive backtracking solver private function _solve($eIndex) { $column = 0; $row = 0; $region = 0; // See if haven't reached the end of the pattern if ($eIndex < count($this->_emptyCells)) { // Get the corresponding candidatelists $this->_getCandidateLists($this->_emptyCells[$eIndex], $column, $row, $region); // Check if $i occurs in all three candidatelists for ($i = 1; $i < 10; $i++) { if ($this->_isCandidate($this->_candidates[$column], $i) && $this->_isCandidate($this->_candidates[$row], $i) && $this->_isCandidate($this->_candidates[$region], $i)) { // Suitable candidate found, use it! $this->sudoku[$this->_emptyCells[$eIndex]] = $i; // Exclude this number from the candidatelists $this->_exclude($this->_candidates[$column], $i); $this->_exclude($this->_candidates[$row], $i); $this->_exclude($this->_candidates[$region], $i); // Don't advance if a solution has been found if ($this->_ready) return; // Advance to the next cell $this->_solve($eIndex + 1); // Don't revert if a solution has been found if ($this->_ready) return; // Reset the cell $this->sudoku[$this->_emptyCells[$eIndex]] = 0; // Put the candidates back in the lists $this->_include($this->_candidates[$column], $i); $this->_include($this->_candidates[$row], $i); $this->_include($this->_candidates[$region], $i); } } } else { // A solution has been found, get out of recursion $this->_ready = true; } } // Obtains the corresponding candidatelist indices private function _getCandidateLists($position, &$column, &$row, &$region) { $column = $position % 9; $row = floor(9 + $position / 9); $region = floor(18 + floor($column / 3) + 3 * floor(($row - 9) / 3)); } // Excludes a number from the list of candidates private function _exclude(&$bitSet, $bit) { $bitSet &= ~(1 << $bit -1); } // Includes a number into the list of candidates private function _include(&$bitSet, $bit) { $bitSet |= (1 << $bit - 1); } // Determines if number occurs in the specified list of candidates private function _isCandidate($bitSet, $bit) { return (($bitSet & (1 << $bit - 1)) == 0) ? false : true; } } ?> |
|
|
| Forum Opties | |
|---|---|
| Forumhop: | |
| Hop naar: | |