-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0036.cpp
70 lines (65 loc) · 1.5 KB
/
0036.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
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool flag = false;
judge(board, 0, flag);
return flag;
}
void judge(vector<vector<char>>& board, int site, bool& flag) {
int row = board.size(), column = board[0].size();
int cur_x = site / column, cur_y = site % column;
if (site == 81) {
flag = true;
return;
}
if (isValid(board, site)) {
judge(board, site + 1, flag);
if (flag) {
return;
}
}
return;
}
bool isValid(vector<vector<char>>& board, int site) {
int row = board.size(), column = board[0].size();
int cur_x = site / column, cur_y = site % column;
//判断行
bool visit_row[10] = { 0 };
for (int i = 0; i < row; ++i) {
if (board[cur_x][i] != '.') {
int num = board[cur_x][i] - '0';
if (visit_row[num]) {
return false;
}
visit_row[num] = 1;
}
}
//判断列
bool visit_column[10] = { 0 };
for (int i = 0; i < row; ++i) {
if (board[i][cur_y] != '.') {
int num = board[i][cur_y] - '0';
if (visit_column[num]) {
return false;
}
visit_column[num] = 1;
}
}
//判断相邻的九块
bool visit_bulk[10] = { 0 };
int start_x = cur_x / 3 * 3, start_y = cur_y / 3 * 3;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
int temp_x = start_x + j, temp_y = start_y + k;
if (board[temp_x][temp_y] != '.') {
int num = board[temp_x][temp_y] - '0';
if (visit_bulk[num]) {
return false;
}
visit_bulk[num] = 1;
}
}
}
return true;
}
};