【问题标题】:Sudoku Solver code gives unexpected result数独求解器代码给出了意想不到的结果
【发布时间】:2022-01-06 18:38:46
【问题描述】:

问题链接:https://leetcode.com/problems/valid-sudoku/description/

以下是我的数独求解器代码。 如果数独是可解的,我应该返回 true,否则返回 false。

class Solution {
public:
    bool solveSudoku(vector<vector<char>> &board, int row, int col) {
        // If the position is now at the end of the 9*9 grid
        if(row == 8 and col == 9) return true;
        // if column has reached upper bound
        if(col==9)  {
            row++;
            col=0;
        }
        // For characters other than dot move to the next position
        if(board[row][col]!='.') return solveSudoku(board,row,col+1); 
        for(int i=1; i<=9; i++) {
            char num='0'+i;
            if(isValid(board, row, col, num)) {
                board[row][col]=num;
                if(solveSudoku(board,row,col+1)) return true;
            }
            board[row][col]='.';
        }
        return false;
    }
    bool isValid(vector<vector<char>> &board, int row, int col, char num) {
        int i,j;
        /* Checking if its duplicated on the same row */
        for(i=0; i<9; i++) {
            if(i!=col && board[row][i] == num) {
                return false;
            }
        }
        /* Checking if its duplicated on the same col */
        for(i=0; i<9; i++) {
            if(i!=row && board[i][col] == num) {
                return false;
            }
        }
        /* Checking if its duplicated inside the 3*3 grid */
        int rowOffset=row-(row%3);
        int colOffset=col-(col%3);
        for(i=0; i<3;i++) {
            for(j=0;j<3;j++) {
                if((rowOffset+i)!=row && (colOffset+j)!=col && board[rowOffset+i][colOffset+j] == num) {
                    return false;
                }
            }
        }
        return true;
    }
    bool isValidSudoku(vector<vector<char>>& board) {
        if(solveSudoku(board, 0, 0)) return true;
        return false;
    }
};

对于下面的测试输入

[[".","8","7",  "6","5","4",  "3","2","1"],
 ["2",".",".",  ".",".",".",  ".",".","."],
 ["3",".",".",  ".",".",".",  ".",".","."],

 ["4",".",".",  ".",".",".",  ".",".","."], 
 ["5",".",".",  ".",".",".",  ".",".","."],
 ["6",".",".",  ".",".",".",  ".",".","."],

 ["7",".",".",  ".",".",".",  ".",".","."],
 ["8",".",".",  ".",".",".",  ".",".","."],
 ["9",".",".",  ".",".",".",  ".",".","."]]

注意:这是一个 leetcode 问题

它返回 false,我认为它是正确的,但预期的答案是 true。这怎么可能是一个错误的答案。 谁能解释一下。

提前致谢

【问题讨论】:

  • 关于标题中信息内容的说明:如果得到了预期的结果,你会问这个问题吗?
  • 你怎么知道预期的答案是真的?如果我正确解释了您的测试输入,那么该数独将无法解决,因为它需要在第一行中使用两个 1s 或在第一列中使用两个 9s。
  • 这是一个leetcode问题
  • “预期答案为真”的来源是什么?问他们。因为,假设我(稍微大胆地)对输入结构进行了适当的格式化,我真的同意“无法解决”。实际上,输入似乎是专门设计为无法解决的......
  • 建议:添加一个main 将输入反馈到问题中,这样您就可以得到更接近minimal reproducible example 的内容,我们都可以运行和玩耍。

标签: c++ recursion backtracking recursive-backtracking


【解决方案1】:

你误解了任务。

确定 9 x 9 数独板是否有效。只有填充的单元格需要根据以下规则进行验证:

填充的单元格不违反数独规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2017-01-05
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多