C++ 数独解算器多种解决方案
下面我有一个9x9数独解算器的大纲,但我不知道如何将多个解算器合并到某个带有部分条目的数独中,如果还没有的话。有人能把这件事告诉我吗 此算法使用回溯,因此使用堆栈C++ 数独解算器多种解决方案,c++,sudoku,C++,Sudoku,下面我有一个9x9数独解算器的大纲,但我不知道如何将多个解算器合并到某个带有部分条目的数独中,如果还没有的话。有人能把这件事告诉我吗 此算法使用回溯,因此使用堆栈 Algorithm findSolutions: Given: (cell, value) findDecidableCell(puzzle) - returns reference to a cell (if any) whose value can be immediately decided alo
Algorithm findSolutions:
Given:
(cell, value) findDecidableCell(puzzle) - returns reference to a cell (if any) whose value
can be immediately decided along that value
void Puzzle::decide(cell, value) - note that value has been decided for cell
bool Puzzle::solved() - return true if the puzzle has been solved
Input:
puzzle - ADT representing the set of possible solutions to current puzzle
strategies[] - list of deductive strategies
Returns:
list of solutions
基本的看起来不错。要找到某个谜题的所有解决方案,您需要做的是,当您找到一个解决方案时,将该解决方案存储在列表中,然后继续,就好像您没有解决方案一样。所以你可以回过头来,尝试另一种猜测。基本面看起来不错。要找到某个谜题的所有解决方案,您需要做的是,当您找到一个解决方案时,将该解决方案存储在列表中,然后继续,就好像您没有解决方案一样。所以你回过头来,再试试另一个猜测。这是我很久以前写的
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iterator>
#include <iomanip>
bool same_row ( int row, int col ) {
return ( (row / 9 )== (col / 9 ));
}
bool same_col(int row, int col ) {
return (((row - col) % 9) == 0);
}
bool same_block(int row, int col) {
return ( (((row/27) == (col/27)))&&(((row % 9)/3) == ((col % 9)/3)));
}
void solve_r(std::vector<int> data) {
std::vector<int>::iterator found = std::find(data.begin(), data.end(), 0);
if ( found == data.end()) {
std::cout << "---+---+---+---+---+---+---+---+---+" << std::endl;
int limit = 0;
for ( std::vector<int>::iterator itr = data.begin(); itr != data.end(); ++itr) {
std::cout << std::setw(3) << *itr << "|" ;
if ( limit == 8 ){
std::cout << std::endl;
std::cout << "---+---+---+---+---+---+---+---+---+" << std::endl;
limit = 0;
} else {
limit++;
}
}
std::cout << std::endl << std::endl;
return;
}
int i = (int)(found - data.begin());
std::set<int> excluded_numbers;
for ( int j = 0; j < 81; j++) {
if ( same_row(i,j) || same_col(i,j) || same_block(i,j)) {
excluded_numbers.insert(data[j]);
}
}
for ( int m = 1; m <= 9; m++) {
std::set<int>::iterator found = excluded_numbers.find(m);
if ( found == excluded_numbers.end()) {
data[i] = m;
solve_r(data);
}
}
}
int main( int argc, char** argv) {
std::ifstream inFile(argv[1]);
if(!inFile) {
exit ( 0 );
}
std::vector<int> data;
std::string str = "";
while(std::getline(inFile, str)) {
data.clear();
for ( std::string::iterator itr = str.begin(); itr != str.end(); ++itr) {
std::string s;
s.push_back(*itr);
std::stringstream ss(s);
int i = 0;
ss >> i;
data.push_back(i);
}
solve_r(data);
}
}
avinash@avinash-laptop:~/work/suduko$ cat 40.inp
096040001100060004504810390007950043030080000405023018010630059059070830003590007
avinash@avinash-laptop:~/work/suduko$ ./suduko 40.inp
---+---+---+---+---+---+---+---+---+
3| 9| 6| 2| 4| 5| 7| 8| 1|
---+---+---+---+---+---+---+---+---+
1| 7| 8| 3| 6| 9| 5| 2| 4|
---+---+---+---+---+---+---+---+---+
5| 2| 4| 8| 1| 7| 3| 9| 6|
---+---+---+---+---+---+---+---+---+
2| 8| 7| 9| 5| 1| 6| 4| 3|
---+---+---+---+---+---+---+---+---+
9| 3| 1| 4| 8| 6| 2| 7| 5|
---+---+---+---+---+---+---+---+---+
4| 6| 5| 7| 2| 3| 9| 1| 8|
---+---+---+---+---+---+---+---+---+
7| 1| 2| 6| 3| 8| 4| 5| 9|
---+---+---+---+---+---+---+---+---+
6| 5| 9| 1| 7| 4| 8| 3| 2|
---+---+---+---+---+---+---+---+---+
8| 4| 3| 5| 9| 2| 1| 6| 7|
---+---+---+---+---+---+---+---+---+
Solving
-------------------------------
User CPU Time : 0 s
System CPU Time: 0 s
Wait Time : 0.001 s
-------------------------------
Elapsed Time : 0.001 s
这是我很久以前写的
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iterator>
#include <iomanip>
bool same_row ( int row, int col ) {
return ( (row / 9 )== (col / 9 ));
}
bool same_col(int row, int col ) {
return (((row - col) % 9) == 0);
}
bool same_block(int row, int col) {
return ( (((row/27) == (col/27)))&&(((row % 9)/3) == ((col % 9)/3)));
}
void solve_r(std::vector<int> data) {
std::vector<int>::iterator found = std::find(data.begin(), data.end(), 0);
if ( found == data.end()) {
std::cout << "---+---+---+---+---+---+---+---+---+" << std::endl;
int limit = 0;
for ( std::vector<int>::iterator itr = data.begin(); itr != data.end(); ++itr) {
std::cout << std::setw(3) << *itr << "|" ;
if ( limit == 8 ){
std::cout << std::endl;
std::cout << "---+---+---+---+---+---+---+---+---+" << std::endl;
limit = 0;
} else {
limit++;
}
}
std::cout << std::endl << std::endl;
return;
}
int i = (int)(found - data.begin());
std::set<int> excluded_numbers;
for ( int j = 0; j < 81; j++) {
if ( same_row(i,j) || same_col(i,j) || same_block(i,j)) {
excluded_numbers.insert(data[j]);
}
}
for ( int m = 1; m <= 9; m++) {
std::set<int>::iterator found = excluded_numbers.find(m);
if ( found == excluded_numbers.end()) {
data[i] = m;
solve_r(data);
}
}
}
int main( int argc, char** argv) {
std::ifstream inFile(argv[1]);
if(!inFile) {
exit ( 0 );
}
std::vector<int> data;
std::string str = "";
while(std::getline(inFile, str)) {
data.clear();
for ( std::string::iterator itr = str.begin(); itr != str.end(); ++itr) {
std::string s;
s.push_back(*itr);
std::stringstream ss(s);
int i = 0;
ss >> i;
data.push_back(i);
}
solve_r(data);
}
}
avinash@avinash-laptop:~/work/suduko$ cat 40.inp
096040001100060004504810390007950043030080000405023018010630059059070830003590007
avinash@avinash-laptop:~/work/suduko$ ./suduko 40.inp
---+---+---+---+---+---+---+---+---+
3| 9| 6| 2| 4| 5| 7| 8| 1|
---+---+---+---+---+---+---+---+---+
1| 7| 8| 3| 6| 9| 5| 2| 4|
---+---+---+---+---+---+---+---+---+
5| 2| 4| 8| 1| 7| 3| 9| 6|
---+---+---+---+---+---+---+---+---+
2| 8| 7| 9| 5| 1| 6| 4| 3|
---+---+---+---+---+---+---+---+---+
9| 3| 1| 4| 8| 6| 2| 7| 5|
---+---+---+---+---+---+---+---+---+
4| 6| 5| 7| 2| 3| 9| 1| 8|
---+---+---+---+---+---+---+---+---+
7| 1| 2| 6| 3| 8| 4| 5| 9|
---+---+---+---+---+---+---+---+---+
6| 5| 9| 1| 7| 4| 8| 3| 2|
---+---+---+---+---+---+---+---+---+
8| 4| 3| 5| 9| 2| 1| 6| 7|
---+---+---+---+---+---+---+---+---+
Solving
-------------------------------
User CPU Time : 0 s
System CPU Time: 0 s
Wait Time : 0.001 s
-------------------------------
Elapsed Time : 0.001 s
这都是伪密码吗?因为你错过了很多分号那都是伪代码?因为您缺少很多分号是的,这都是伪代码。^对艾尔克来说,一旦我找到了一个解决方案,我该如何继续?我必须改变处理下一个解决方案的方式,否则我会得到与以前相同的解决方案,对吗?这就是让这一切变得复杂的原因。我假设您的简化方法存储了单元格中有效的值。只需删除正确的值,就可以防止您再次尝试相同的答案。是的,这都是psuedo代码。^对艾尔克来说,一旦我找到了一个解决方案,我该如何继续?我必须改变处理下一个解决方案的方式,否则我会得到与以前相同的解决方案,对吗?这就是让这一切变得复杂的原因。我假设您的简化方法存储了单元格中有效的值。只需删除正确的值,就可以防止您再次尝试相同的答案;我不认为它解决了这个问题。我很确定这个解算器将无法解决一些骗子数独;我不认为它解决了这个问题。