-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj2239.cpp
67 lines (66 loc) · 1.56 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
#include <bits/stdc++.h>
using namespace std;
bool col[10][10]={false,};
bool row[10][10]={false,};
bool box[10][10]={false,};
int doku[10][10];
int answer[10][10];
int tmp[10][10];
void input(){
for(int i=1;i<10;i++){
for(int j=1;j<10;j++){
char tmp;
cin >> tmp;
doku[i][j]=tmp-'0';
if(doku[i][j]!=0){
col[j][doku[i][j]]=true;
row[i][doku[i][j]]=true;
box[(i-1)/3*3+(j-1)/3+1][doku[i][j]]=true;
}
}
}
}
void backt(int x, int y){
if(x==10 && y==1){
for(int i =1; i<10; i++){
for(int j =1;j<10;j++){
cout << doku[i][j];
}
cout << "\n";
}
exit(0);
}
else if(doku[x][y]==0){
for(int i =1; i<10; i++){
if(col[y][i]==false&&row[x][i]==false&&box[(x-1)/3*3+(y-1)/3+1][i]==false){
col[y][i]=true;
row[x][i]=true;
box[(x-1)/3*3+(y-1)/3+1][i]=true;
doku[x][y]=i;
if(y==9){
backt(x+1,1);
}
else{
backt(x,y+1);
}
doku[x][y]=0;
col[y][i]=false;
row[x][i]=false;
box[(x-1)/3*3+(y-1)/3+1][i]=false;
}
}
}
else{
if(y==9){
backt(x+1,1);
}
else{
backt(x,y+1);
}
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
input();
backt(1,1);
}