-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ2239.cpp
99 lines (90 loc) · 1.75 KB
/
BOJ2239.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by 전형진 on 2020-03-18.
//
#include <stdio.h>
#include <vector>
using namespace std;
struct POS{
int y;
int x;
};
vector<POS> v;
int map[9][9];
bool flag = false;
bool check1(int x,int num){
for (int i = 0; i < 9; ++i) {
if(map[i][x] == num)
return false;
}
return true;
}
bool check2(int y,int num){
for (int i = 0; i < 9; ++i) {
if(map[y][i] == num)
return false;
}
return true;
}
bool check3(int y,int x,int num){
int tempx = 0;
int tempy = 0;
if(y/3 == 0){
tempy = 0;
}
else if(y/3 == 1){
tempy = 3;
}
else if(y/3 == 2){
tempy = 6;
}
if(x/3 == 0){
tempx = 0;
}
else if(x/3 == 1){
tempx = 3;
}
else if(x/3 == 2){
tempx = 6;
}
for (int i = tempy; i < tempy+3 ; ++i) {
for (int j = tempx; j < tempx+3; ++j) {
if(map[i][j] == num){
return false;
}
}
}
return true;
}
void DFS(int depth){
if(flag) return;
if(depth == v.size()){
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
printf("%d",map[i][j]);
}
printf("\n");
}
flag = true;
return;
}
int y = v[depth].y;
int x = v[depth].x;
for (int i = 1; i <= 9; ++i) {
if(check1(x,i) && check2(y,i) && check3(y,x,i)){
map[y][x] = i;
DFS(depth+1);
map[y][x] = 0;
}
}
}
int main(){
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
scanf("%1d", &map[i][j]);
if (map[i][j] == 0) {
v.push_back({i, j});
}
}
}
DFS(0);
}