void BackTrack(vector > &board,int k,bool& bFindOne)
{
if(k>=81)
{
bFindOne=true;//find a solution
return;
}
int row=k/9;
int col=k%9;
if(isdigit(board[row][col]))//already set
BackTrack(board,k+1,bFindOne);
else
{
for(int i=1;i<=9;i++)
{
board[row][col]=i+'0';
if(isValid(board,row,col))
BackTrack(board,k+1,bFindOne);
if(bFindOne)return;
}
board[row][col]='.';//reset this position before trackback
}
}
void solveSudoku(vector > &board)
{
bool bFindOne=false;
BackTrack(board,0,bFindOne);
}
加上sudoku validation 的判断代码,确实太长了