-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0037.解数独.cpp
140 lines (132 loc) · 4.03 KB
/
0037.解数独.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* @lc app=leetcode.cn id=37 lang=cpp
*
* [37] 解数独
*
* https://leetcode-cn.com/problems/sudoku-solver/description/
*
* algorithms
* Hard (55.87%)
* Likes: 265
* Dislikes: 0
* Total Accepted: 13.7K
* Total Submissions: 23.8K
* Testcase Example: '[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]'
*
* 编写一个程序,通过已填充的空格来解决数独问题。
*
* 一个数独的解法需遵循如下规则:
*
*
* 数字 1-9 在每一行只能出现一次。
* 数字 1-9 在每一列只能出现一次。
* 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
*
*
* 空白格用 '.' 表示。
*
*
*
* 一个数独。
*
*
*
* 答案被标成红色。
*
* Note:
*
*
* 给定的数独序列只包含数字 1-9 和字符 '.' 。
* 你可以假设给定的数独只有唯一解。
* 给定数独永远是 9x9 形式的。
*
*
*/
// @lc code=start
#include <map>
#include <vector>
using namespace std;
class Solution {
public:
void backtrace(vector<int>* row, vector<int>* col, vector<int>* mat, bool& is_done,
vector<vector<char>>& board, int n_row, int n_col)
{
if (is_done)
return;
if (n_row == 9) {
is_done = true;
return;
}
int next_row = n_col == 8 ? (n_row + 1) : n_row;
int next_col = n_col == 8 ? 0 : n_col + 1;
if (board[n_row][n_col] != '.') {
backtrace(row, col, mat, is_done, board, next_row, next_col);
return;
}
for (int i = 1; i < 10; i++) {
if (row[n_row][i - 1] != 1 && col[n_col][i - 1] != 1 && mat[(n_col / 3) + (n_row / 3) * 3][i - 1] != 1) {
board[n_row][n_col] = '0' + i;
row[n_row][i - 1] = 1;
col[n_col][i - 1] = 1;
mat[(n_col / 3) + (n_row / 3) * 3][i - 1] = 1;
backtrace(row, col, mat, is_done, board, next_row, next_col);
if (is_done) {
return;
}
board[n_row][n_col] = '.';
row[n_row][i - 1] = 0;
col[n_col][i - 1] = 0;
mat[(n_col / 3) + (n_row / 3) * 3][i - 1] = 0;
}
}
}
void solveSudoku(vector<vector<char>>& board)
{
// 用 vector 而不是 map 来加快速度
vector<int> row[9] = {
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
};
vector<int> col[9] = {
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
};
vector<int> mat[9] = {
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
vector<int>(9, 0),
};
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] != '.') {
row[i][board[i][j] - '0' - 1] = 1;
col[j][board[i][j] - '0' - 1] = 1;
mat[(j / 3) + (i / 3) * 3][board[i][j] - '0' - 1] = 1;
}
}
}
bool is_done = false;
backtrace(row, col, mat, is_done, board, 0, 0);
}
};
// @lc code=end