1刷
题目也是很简单,判断用了上一题的代码,dfs一次ac!!!!主要是写多了一个小东西一直bug
Else 后面多了东西
还有map<>m[10]这个东西传参解决不了,大神说可以用vector存map,我后来是直接用了全局变量23333333
class Solution {
public:
bool isOK(vector<vector<char>>& board){
for(int i = 0; i < 9; ++ i){
unordered_map<char, bool> mm1;
unordered_map<char, bool> mm2;
unordered_map<char, bool> mm3;
for(int j = 0; j < 9; ++ j){
if(mm1[board[i][j]] == true)
return false;
mm1[board[i][j]] = true;
if(mm2[board[j][i]] == true)
return false;
mm2[board[j][i]] = true;
if(mm3[board[i/3*3+j/3][i%3*3+j%3]] == true)
return false;
mm3[board[i/3*3+j/3][i%3*3+j%3]] = true;
}
}
return true;
}
unordered_map<char, bool>m1[9];
unordered_map<char, bool>m2[9];
unordered_map<char, bool>m3[9];
bool numisOK(char t, int i, int j){
if(m1[i][t] != true && m2[j][t] != true && m3[i/3*3+j/3][t] != true)
return true;
return false;
}
int f = 1;
void solveit(vector<vector<char>>& board, int a, int b){
if(!f) return;
if(a == 9){
if(isOK(board)) f = 0;
return;
}
if(board[a][b] != '.'){
if(b == 8)
solveit(board, a + 1, 0);
else
solveit(board, a, b + 1);
return;
}
for(int i = 1; i <= 9; ++ i){
char c = i + 48;
if(numisOK(c, a, b)){
m1[a][c] = true;
m2[b][c] = true;
m3[a/3*3+b/3][c] = true;
board[a][b] = c;
if(b == 8)
solveit(board, a + 1, 0);
else
solveit(board, a, b + 1);
if(!f) return;
m1[a][c] = false;
m2[b][c] = false;
m3[a/3*3+b/3][c] = false;
board[a][b] = '.';
}
else
continue;
}
return;
}
void solveSudoku(vector<vector<char>>& board) {
for(int i = 0; i < 9; ++ i){
for(int j = 0; j < 9; ++ j){
if(board[i][j] != '.'){
m1[i][board[i][j]] = true;
m2[j][board[i][j]] = true;
m3[i/3*3+j/3][board[i][j]] = true;
}
}
}
solveit(board, 0, 0);
}
};
2刷
没有2刷因为1刷一次就ac了,简单dfs,3刷可以看看一个题解为2ms的代码,自己的代码是116ms,,差了50倍!