数独解算器程序

时间:2017-02-18 21:11:10

标签: c++ backtracking sudoku

boundItemClick(idx, e) { // add idx as argument console.log(idx); } render() { var data=[]; data.push (<div> <span>{textLabel} onClick={this.boundItemClick.bind(this, data.length)}</span> </div> ); } 函数调用

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;
}

预期输出

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 

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 函数中调用solveSudoku时,输入数据作为参数。它由main()19的字符组成,代表空字符。 .函数的作用是正确填充数独中的所有元素(更改solveSudoku中的值)。但我得到了错误的答案。给出输入数据是可解决的。

正如Fezvez所说,我也认为算法中存在的问题在于A。我认为在用有效字符填充单元格之后,如果右侧,下方和对角线上的块也被填充,则该语句应该递归检查。如果是,则数据被解决并且它应该返回true但是如果三个中的任何一个失败则它应该回溯。但为什么不这样做呢?

2 个答案:

答案 0 :(得分:4)

重新完成答案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))的行为与您的想法不同。您应该使用深度优先搜索探索进行回溯。但是你没有这样做:sudoku(A, (i+1)%9, j+(i+1)/9)既不是BFS也不是DFS,它会使你的算法失败

这是一个稍微修改过的版本,我用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)。您从sudoku(A,0,1)开始并返回true。你现在已经填满了几乎所有的A(顶行除外)。您前进到计算sudoku(A, i, j),但如果您之前几乎完全填充实际上无效(无法填充顶行),您的算法会立即失败
  • 换句话说,您的代码失败,因为调用if有副作用(在A中写入数据),当您在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; }

以下是使用您的示例更新的代码

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

<强>输出

AWS_IAM

答案 1 :(得分:-2)

你需要一个堆栈。然后你需要彻底尝试1-9,如果一切都无效就放松。如果全部无效,则需要继续上一级别,如果1-9全部无效,则再次展开。

但这是一个毫无希望的算法。虽然它最终适用于简单的数独游戏,但执行起来只需要很长时间。