-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuduko.cpp
More file actions
111 lines (92 loc) · 2.45 KB
/
Copy pathSuduko.cpp
File metadata and controls
111 lines (92 loc) · 2.45 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
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
100
101
102
103
104
105
106
107
108
109
110
111
#include<bits/stdc++.h>
using namespace std;
vector<unordered_set<int>> box(9), row(9), col(9);
int whichBoxRow(int i){
if(i < 3) return 0;
if(i < 6) return 1;
return 2;
}
int whichBoxCol(int j){
if(j < 3) return 0;
if(j < 6) return 1;
return 2;
}
int whichBox(int i, int j){
int row = whichBoxRow(i);
int col = whichBoxCol(j);
return row*3+col;
}
bool validate(){
// cout<<"validaing "<<endl;
for(int i=0; i<9;i++){
// cout<<i<<" "<<row[i].size()<<" "<<col[i].size()<<" "<<box[i].size()<<" "<<endl;
if(row[i].size() != 9) return false;
if(col[i].size() != 9) return false;
if(box[i].size() != 9) return false;
}
return true;
}
void print(vector<vector<int>> &mat){
for(int i=0; i<9; i++){
for(int j=0; j<9; j++) cout<<mat[i][j]<<" ";
cout<<endl;
}
}
bool solve(vector<vector<int>> &mat, int x, int y){
if(y >= 9){
x++;
y = 0;
}
if(x >= 9) {
print(mat);
return validate();
}
if(mat[x][y] == 0){
for(int no = 1; no <= 9; no++){
if(row[x].find(no) == row[x].end() && col[y].find(no) == col[y].end()
&& box[whichBox(x, y)].find(no) == box[whichBox(x, y)].end()){
row[x].insert(no);
col[y].insert(no);
box[whichBox(x,y)].insert(no);
mat[x][y] = no;
// cout<<"placing "<<x<<" "<<y<<" "<<no<<endl;
if(solve(mat, x, y+1)) return true;
mat[x][y] = 0;
row[x].erase(no);
col[y].erase(no);
box[whichBox(x,y)].erase(no);
}
}
}
else {
if(solve(mat, x, y+1)) return true;
}
return false;
}
int main(){
// write your code here
int n = 9;
vector<vector<int>> mat(n, vector<int> (n));
for(int i=0; i<n; i++){
for(int j=0; j<n; j++) {
cin>>mat[i][j];
if(mat[i][j]) {
row[i].insert(mat[i][j]);
col[j].insert(mat[i][j]);
}
}
}
for(int i=0; i<9; i+=3){
for(int j=0; j<9; j+=3){
for(int x = i; x < i+3; x++){
for(int y = j; y < j+3; y++){
if(mat[x][y]) box[whichBox(x,y)].insert(mat[x][y]);
}
}
}
}
if(solve(mat, 0, 0)) cout<<"true";
else cout<<"false";
cout<<endl;
return 0;
}