-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1025.cpp
More file actions
60 lines (60 loc) · 1.42 KB
/
1025.cpp
File metadata and controls
60 lines (60 loc) · 1.42 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
#include <bits/stdc++.h>
using namespace std;
int G[9][9], nine[10], times = 0;
inline bool chk(int x, int y, int c) {
memset(nine, 0, sizeof(nine));
nine[c] += 1;
for (int i = 0; i < 9; i++) nine[G[i][y]]++;
for (int i = 1; i <= 9; i++)
if (nine[i] > 1) return 0;
memset(nine, 0, sizeof(nine));
nine[c] += 1;
for (int i = 0; i < 9; i++) nine[G[x][i]]++;
for (int i = 1; i <= 9; i++)
if (nine[i] > 1) return 0;
memset(nine, 0, sizeof(nine));
nine[c] += 1;
for (int i = (x / 3) * 3; i < (x / 3) * 3 + 3; i++)
for (int j = (y / 3) * 3; j < (y / 3) * 3 + 3; j++) nine[G[i][j]]++;
for (int i = 1; i <= 9; i++)
if (nine[i] > 1) return 0;
return 1;
}
void f(int x, int y) {
if (x == 9 && y == 0) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) cout << G[i][j] << ' ';
cout << '\n';
}
cout << '\n';
times++;
return;
}
int _x = x, _y = y + 1;
_x += _y / 9;
_y = _y % 9;
if (!G[x][y]) {
for (int k = 1; k <= 9; k++) {
if (chk(x, y, k)) {
G[x][y] = k;
f(_x, _y);
} else {
if (k == 9) {
G[x][y] = 0;
} else
continue;
}
G[x][y] = 0;
}
} else
f(_x, _y);
}
main(void) {
cin.tie(0);
ios_base::sync_with_stdio(0);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) cin >> G[i][j];
f(0, 0);
cout << "there are a total of " << times << " solution(s).\n";
return 0;
}