-
Notifications
You must be signed in to change notification settings - Fork 2
/
1106.cpp
65 lines (59 loc) · 1.96 KB
/
1106.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
#include <iostream>
#include <cstring>
using namespace std;
bool block[10][3][3], row[10][9], col[10][9];
bool isOk(int n, int i, int j) {
return (block[n][i / 3][j / 3] && row[n][i] && col[n][j]);
}
int ansNum(int arr[][9], int i, int j) {
int count = 0;
if (i == 9) return 1;//终止条件。
if (arr[i][j] == 0) {//为0才填入。
for (int n = 1;n <= 9;++n) {
if (isOk(n, i, j)) {
block[n][i / 3][j / 3] = row[n][i] = col[n][j] = false;
switch (ansNum(arr, i + (j + 1) / 9, (j + 1) % 9)) {
case 1:
if (++count > 1) return 2;
break;
case 2:
return 2;
}
block[n][i / 3][j / 3] = row[n][i] = col[n][j] = true;//回溯。
}
}
return count;
}
else return ansNum(arr, i + (j + 1) / 9, (j + 1) % 9);
}
bool initBool(int arr[][9]) {
int count = 0;
for (int n = 1;n <= 9;++n) {
int i, j;
for (i = 0;i < 3;++i)
for (j = 0;j < 3;++j)
block[n][i][j] = true;
for (i = 0;i < 9;++i) row[n][i] = col[n][i] = true;
for (i = 0;i<9;++i)
for (j = 0;j<9;++j)
if (arr[i][j] == n) {
++count;
if (!block[n][i / 3][j / 3] || !row[n][i] || !col[n][j]) return false;//检查初始数据是否违规。
block[n][i / 3][j / 3] = row[n][i] = col[n][j] = false;
}
}
return (count >= 17);//至少17个已知数据。
}
int main() {
ios::sync_with_stdio(false);
int T; cin >> T;
int sudoku[9][9];
memset(sudoku, sizeof(sudoku), 0);
for (int i = 0; i < T; ++i) {
for (int j = 0; j < 9; ++j)
for (int k = 0; k < 9; ++k) cin >> sudoku[j][k];
if (!initBool(sudoku)) cout << "No" << endl;
else cout << (ansNum(sudoku, 0, 0) == 1 ? "Yes" : "No") << endl;
}
return 0;
}