-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA-989.cpp
More file actions
74 lines (68 loc) · 1.63 KB
/
UVA-989.cpp
File metadata and controls
74 lines (68 loc) · 1.63 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
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int n, divi;
int ans;
int s[N][N];
int linha[N][N], coluna[N][N];
int box[N][N];
// linha[x][y] = checa ocorrencia do numero Y na linha X
// box[x][y] = ocorrencia de 'y' na x-esima caixinha
int boxId(int x, int y) {
int colunaMaior = y / divi;
int linhaMaior = x / divi;
return linhaMaior * divi + colunaMaior;
}
void bt(int x, int y) {
if (x == n - 1 && y == n) {
ans = 1;
return;
}
if (y == n) bt(x + 1, 0);
else if (s[x][y] != 0) bt(x, y + 1);
else {
for (int i = 1; i <= 9; ++i) {
if (linha[x][i]) continue;
if (coluna[y][i]) continue;
if (box[boxId(x, y)][i]) continue;
s[x][y] = i;
linha[x][i] = 1; coluna[y][i] = 1; box[boxId(x, y)][i] = 1;
bt(x, y + 1);
if (ans) return;
s[x][y] = 0;
linha[x][i] = 0; coluna[y][i] = 0; box[boxId(x, y)][i] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(nullptr);
int ja = 0;
while (cin >> n) {
ans = 0;
divi = n;
n *= n;
memset(box, 0, sizeof box);
memset(linha, 0, sizeof linha);
memset(coluna, 0, sizeof coluna);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> s[i][j];
if (s[i][j] != 0) {
linha[i][s[i][j]] = coluna[j][s[i][j]] = box[boxId(i, j)][s[i][j]] = 1;
}
}
}
bt(0, 0);
if (!ja) ja = 1;
else cout << endl;
if (!ans) cout << "NO SOLUTION" << endl;
else {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << s[i][j] << " \n"[j == n - 1];
}
}
}
}
return 0;
}