Gya*_*shu 6 c++ sudoku backtracking
solveSudoku从main()函数调用函数.
我已经编写了以下函数来解决数独:
#include <iostream>
#include <vector>
using namespace std;
int isvalid(char k, vector<vector<char> > A, int i, int j) { //Checking if putting the current element is not in same row, column or box
for(int t = 0; t < 9; t++) {
if(A[t][j] == k) //Checking jth column
return 0;
if(A[i][t] == k) //Checking ith row
return 0;
if(A[(i/3)*3+t/3][(j/3)*3+t%3] == k) //Checking current box
return 0;
}
return 1;
}
bool sudoku(vector<vector<char> > &A, int i, int j) {
if(i > 8 || j > 8) //If coordinates of the matrix goes out of bounds return true
return true;
if(A[i][j] == '.') {
for(char k = '1'; k <= '9'; k++) { //Trying to put every character possible
if(isvalid(k, A, i, j)) { //If putting character `k` doesn't makes the sudoku invaild put it
A[i][j] = k;
if(sudoku(A, i+1, j) && sudoku(A, i, j+1) && sudoku(A, i+1, j+1))//Check further if the sudoku can be solved with that configuration by going to the right block, down block and bottom-right block
return true;
else
A[i][j] = '.'; //Reset(If the sudoku can't be solved with putting `k` in `i, j` th index replace the '.' character at that position)
}
}
}
else {
if(sudoku(A, i+1, j) && sudoku(A, i, j+1) && sudoku(A, i+1, j+1))
return true;
}
return false;//This should trigger backtracking
}
void solveSudoku(vector<vector<char> > &A) {
sudoku(A, 0, 0);
}
int main() {
vector<vector<char> > A = {{'5','3','.','.','7','.','.','.','.'}, {'6','.','.','1','9','5','.','.','.'}, {'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'}, {'4','.','.','8','.','3','.','.','1'}, {'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'}, {'.','.','.','4','1','9','.','.','5'}, {'.','.','.','.','8','.','.','7','9'}}; //Input sudoku
solveSudoku(A);
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
cout<<A[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT
5 3 . . 7 . . . .
6 . . 1 9 5 . . .
. 9 8 . . . . 6 .
8 . . . 6 . . . 3
4 . . 8 . 3 . . 1
7 . . . 2 . . . 6
. 6 . . . . 2 8 .
. . . 4 1 9 . . 5
3 1 4 5 8 2 6 7 9
Run Code Online (Sandbox Code Playgroud)
预期产出
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Run Code Online (Sandbox Code Playgroud)
solveSudoku在main()函数中调用时,输入sudoku作为参数给出.它包含的字符从1以9和.代表空字符.solveSudokufunction的工作是正确填充sudoku中的所有元素(A在适当的位置更改值).但我得到了错误的答案.给出输入数据是可解的.
正如Fezvez所说,我也认为算法中的问题在于这个陈述if(sudoku(A, i+1, j) && sudoku(A, i, j+1) && sudoku(A, i+1, j+1)).我认为在用有效字符填充单元格之后,如果右侧,下方和对角线上的块也被填充,则该语句应该递归检查.如果是,则数据被解决并且它应该返回true但是如果三个中的任何一个失败则它应该回溯.但为什么不这样做呢?
重做答案:sudoku(A, i, j)有在A. 当您调用if(sudoku(A, i+1, j) && sudoku(A, i, j+1) && sudoku(A, i+1, j+1)),并点击第二个检查时sudoku(A, i, j+1),情况不再相同A,并且您没有测试您的想法。我通过更改您if出现的两行并只执行一项检查来修复它:sudoku(A, (i+1)%9, j+(i+1)/9)
旧答案:您的代码失败,因为sudoku行为不像您想象的那样。您应该通过深度优先搜索探索进行回溯。但是您没有这样做:if(sudoku(A, i+1, j) && sudoku(A, i, j+1) && sudoku(A, i+1, j+1))既不是 BFS 也不是 DFS,它会使您的算法失败
这是一个稍微修改过的版本,我用sudoku(A, (i+1)%9, j+(i+1)/9)它替换了有问题的部分,它可以工作。
编辑:if(sudoku(A, i+1, j) && sudoku(A, i, j+1) && sudoku(A, i+1, j+1))由于以下原因是违法者:
sudoku(A, i, j)如果从 (i,j) 到右下角的任何矩形包含有效填充,则为真。即您可以输入数字并且它们不违反数独规则。确实,您要计算的是sudoku(A,0,0)if(sudoku(A,1,0) && sudoku(A,0,1) && sudoku(A,1,1))。您开始sudoku(A, 1, 0)并返回 true。您现在几乎填充了所有 A(顶行除外)。您继续计算,sudoku(A,0,1)但如果您之前几乎完成的填充实际上无效(无法填充顶行),您的算法会立即失败sudoku(A, i, j)有副作用(在 A 中写入数据),并且当您点击第三个布尔值中的第二个时,您if没有处理正确的A这是使用您的示例更新的代码
#include <iostream>
#include <vector>
using namespace std;
int isvalid(char k, vector<vector<char> > A, int i, int j) { //Checking if putting the current element is not in same row, column or box
for(int t = 0; t < 9; t++) {
if(A[t][j] == k) //Checking jth column
return 0;
if(A[i][t] == k) //Checking ith row
return 0;
if(A[(i/3)*3+t/3][(j/3)*3+t%3] == k) //Checking current box
return 0;
}
return 1;
}
bool sudoku(vector<vector<char> > &A, int i, int j) {
if(i > 8 || j > 8) //If coordinates of the matrix goes out of bounds return true
return true;
if(A[i][j] == '.') {
for(char k = '1'; k <= '9'; k++) { //Trying to put every character possible
if(isvalid(k, A, i, j)) { //If putting character `k` doesn't makes the sudoku invaild put it
A[i][j] = k;
if(sudoku(A, (i+1)%9, j+(i+1)/9))// CHANGE ONE
return true;
else
A[i][j] = '.'; //Reset(If the sudoku can't be solved with putting `k` in `i, j` th index replace the '.' character at that position)
}
}
}
else {
if(sudoku(A, (i+1)%9, j+(i+1)/9)) // CHANGE TWO
return true;
}
return false;//This should trigger backtracking
}
void solveSudoku(vector<vector<char> > &A) {
sudoku(A, 0, 0);
}
int main() {
vector<vector<char> > A = {{'5','3','.','.','7','.','.','.','.'}, {'6','.','.','1','9','5','.','.','.'}, {'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'}, {'4','.','.','8','.','3','.','.','1'}, {'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'}, {'.','.','.','4','1','9','.','.','5'}, {'.','.','.','.','8','.','.','7','9'}}; //Input sudoku
solveSudoku(A);
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
cout<<A[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Run Code Online (Sandbox Code Playgroud)