leetcode37:解数独
作者:互联网
class Solution {
public:
bool Sudoku(vector<vector<char>>& board){
map<char,int> m;
bool flag=false;
for(int i=1;i<=9;i++){
m['0'+i]+=1;
}
int i,j;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
if(board[i][j]=='.'){
for(int k=0;k<9;k++){
if(board[i][k]!='.') m[board[i][k]]-=1;
if(board[k][j]!='.') m[board[k][j]]-=1;
}
for(int k=i/3*3;k<i/3*3+3;k++){
for(int t=j/3*3;t<j/3*3+3;t++){
if(board[k][t]!='.') m[board[k][t]]-=1;
}
}
bool ok=false;
for(int k=1;k<=9;k++){
if(m['0'+k]>0){
ok=true;
board[i][j]='0'+k;
if(Sudoku(board)) return true;
else board[i][j]='.';
}
}
if(!ok||board[i][j]=='.') return false;
break;
}
}
}
if(i==9) flag=true;
return flag;
}
void solveSudoku(vector<vector<char>>& board) {
Sudoku(board);
}
};
由于数独固定9*9,大小不大,暴力回溯即可。
标签:Sudoku,false,int,return,bool,board,leetcode37,解数 来源: https://blog.csdn.net/SJTU_liangge/article/details/113815407