Determine whether a Sudoku has a unique solution

后端 未结 2 786
北海茫月
北海茫月 2021-01-25 11:14

I\'m struggling with a backtracking algorithm to determine wether a Sudoku has a unique solution or if it has multiple Solutions. Here\'s the backtracking code i use:

         


        
2条回答
  •  盖世英雄少女心
    2021-01-25 11:49

    The reset must be inside the for loop and after the if solve condition

     for (int val = 1; val <= 9; ++val) {
            if (legal(i,j,val,cells)) {
                cells[i][j] = val;
                if (solve(i+1,j,cells))
                    return true;
                cells[i][j] = 0; // reset on backtrack
            }
        }
    

提交回复
热议问题