【发布时间】:2019-11-22 08:28:05
【问题描述】:
我正在尝试编写具有递归和回溯的数独求解器。但我的代码总是返回 false。 SudokuSolver() 仅遍历第一行到第四列,然后停止并开始回溯。问题是回溯一直持续到第一个单元格并最终返回 false。结果,没有一个空单元格(值为 "-1" 的单元格)被任何其他数字 (1-9) 替换,并且棋盘保持原样。
编译器显示 [Done] 在 0.557 秒内以 code=0 退出
#include<iostream>
using namespace std;
# define N 9
bool SolveSudoku();
pair<int,int> FindUnassigned();
void PrintBoard();
bool NoConflict(int, int, int);
bool inRow(int,int);
bool inCol(int, int);
bool inGrid(int, int, int);
bool isFilled(pair<int,int>);
//board[row][col]==-1 indicates an empty position
int board[N][N] = {{3,-1,6,5,-1,8,4,-1,-1},
{5,2,-1,-1,-1,-1,-1,-1,-1},
{-1,5,7,-1,-1,-1,-1,3,1},
{-1,-1,3,-1,1,-1,-1,8,-1},
{9,-1,-1,8,6,3,-1,-1,5},
{-1,5,-1,-1,9,-1,6,-1,-1},
{1,3,-1,-1,-1,-1,2,5,-1},
{-1,-1,-1,-1,-1,-1,-1,7,4},
{-1,-1,5,2,-1,6,3,-1,-1}};
int main() {
if(SolveSudoku())
PrintBoard();
return 0;
}
bool SolveSudoku() {
pair<int,int> pos = FindUnassigned();
int row=pos.first;
int col=pos.second;
if(isFilled(pos)) { return true; }
for(int num=1; num<=9; num++) {
if(NoConflict(num,row,col)) {
board[row][col]=num;
SolveSudoku();
board[row][col]=-1;
}
}
return false;
}
pair<int,int> FindUnassigned() {
pair<int,int> pos;
pos.first=-1;
pos.second=-1;
for(int r=0; r<N; r++) {
for(int c=0; c<N; c++) {
if(board[r][c]==-1) {
pos.first=r;
pos.second=c;
return pos;
}
}
}
return pos;
}
bool isFilled(pair<int,int> pos) {
return (pos.first==-1 && pos.second==-1);
}
bool NoConflict(int num, int r, int c) {
return ((!inRow(numenter code here,r)==false) && (!inCol(num,c)==false) && (!inGrid(num,r-r%3,c-c%3)==false));
}
bool inRow(int num, int r) {
for(int c=0; c<N; c++) {
if(board[r][c]==num) return true;
}
return false;
}
bool inCol(int num, int c) {
for(int r=0; r<N; r++) {
if(board[r][c]==num) return true;
}
return false;
}
bool inGrid(int num, int rf, int cf) {
for(int r=0; r<3; r++) {
for(int c=0; c<3; c++) {
if(board[r+rf][c+cf]==num) return true;
}
}
return false;
}
void PrintBoard() {
for(int r=0; r<N; r++) {
for(int c=0; c<N; c++) {
cout<<board[r][c]<<"\t";
}
enter code here cout<<endl;
}
}
【问题讨论】:
-
也许只有我一个人,但如果位置为空,您的 isFilled() 方法似乎返回 true,这对我来说听起来倒退?
-
IsFilled() 获取一个位置(pair
) 并在 pos=(-1,-1)
bool isFilled(pair<int,int> pos) { return (pos.first==-1 && pos.second==-1); }FindUnassigned() 返回具有该位置的位置变量 pos 时返回 true (row,col) 为空的单元格(cell value=-1) 当板被填满时,不会有任何空单元格(cell value=-1) 并且 FindUnassigned 返回 pos 为 (-1,-1 ) -
我明白了,所以我想我是说函数的命名是“向后” isFilled 在位置为空时返回 true...
标签: c++ sudoku recursive-backtracking