-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ2580_스도쿠.cpp
More file actions
61 lines (60 loc) · 1.36 KB
/
BOJ2580_스도쿠.cpp
File metadata and controls
61 lines (60 loc) · 1.36 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
//
// main.cpp
// BOJ2580_스도쿠
//
// Created by 신지식 on 2018. 10. 9..
// Copyright © 2018년 Shin Ji Sik. All rights reserved.
//
#include <iostream>
using namespace std;
int a[10][10];
bool c[10][10];
bool c2[10][10];
bool c3[10][10];
int n=9;
int square(int x, int y) {
return (x/3)*3+(y/3);
}
bool go(int z) {
if (z == 81) {
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout << a[i][j] << ' ';
}
cout << '\n';
}
return true;
}
int x = z/n;
int y = z%n;
if (a[x][y] != 0) {
return go(z+1);
} else {
for (int i=1; i<=9; i++) {
if (c[x][i] == 0 && c2[y][i] == 0 && c3[square(x,y)][i]==0) {
c[x][i] = c2[y][i] = c3[square(x,y)][i] = true;
a[x][y] = i;
if (go(z+1)) {
return true;
}
a[x][y] = 0;
c[x][i] = c2[y][i] = c3[square(x,y)][i] = false;
}
}
}
return false;
}
int main() {
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cin >> a[i][j];
if (a[i][j] != 0) {
c[i][a[i][j]] = true;
c2[j][a[i][j]] = true;
c3[square(i,j)][a[i][j]] = true;
}
}
}
go(0);
return 0;
}