-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathleetcode_0037_Sudoku_Solver.cpp
More file actions
64 lines (58 loc) · 1.54 KB
/
leetcode_0037_Sudoku_Solver.cpp
File metadata and controls
64 lines (58 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* AC:
* Runtime: 1452 ms, faster than 5.04% of C++ online submissions for Sudoku Solver.
* Memory Usage: 324.9 MB, less than 6.90% of C++ online submissions for Sudoku Solver.
*
* 思路:朴实的 DFS 法,效率不高。。。
*/
class Solution {
public:
bool Check(int n, int key, vector<vector<char>> board) {
char temp = key + 48;
for(int i = 0; i < 9; i++) { // 横坐标是否合法
int j = n / 9;
if(board[j][i] == temp)
return false;
}
for(int i = 0; i < 9; i++) { // 纵坐标是否合法
int j = n % 9;
if(board[i][j] == temp)
return false;
}
int x = n / 9 / 3 * 3;
int y = n % 9 / 3 * 3;
for(int i = x; i < x + 3; i++) { // 小宫格是否合法
for(int j = y; j < y + 3; j++) {
if(board[i][j] == temp)
return false;
}
}
return true;
}
int DFS(int n, vector<vector<char>> & board, bool &isSuccess) {
if(n > 80) {
isSuccess = true;
return 0;
}
if(board[n / 9][n % 9] != '.') {
DFS(n + 1, board, isSuccess);
}
else {
for(int i = 1; i <= 9; i++) {
if(Check(n, i, board) == true) {
char temp = i + 48;
board[n / 9][n % 9] = temp;
DFS(n + 1, board, isSuccess);
if(isSuccess == true)
return 0;
board[n / 9][n % 9] = '.';
}
}
}
return 0;
}
void solveSudoku(vector<vector<char>>& board) {
bool isSuccess = false; // 数独是否已被解决
DFS(0, board, isSuccess);
}
};