-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku Solver.cpp
157 lines (128 loc) · 3.76 KB
/
Sudoku Solver.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Developed by Rahul Surana
***********************************************************
Here I am Going to use dfs solution to find a correct
Solution for a sudoku Puzzle with some given inputs and Stats
Input Format:
-> First input is N which is No of sudoku inputs already given
-> Next N line would be x, y, k which are
xth row ( 1 <= x <= 9) and
yth Column ( 1 <= y <= 9) and
k No to be filled ( 1 <= k <= 9)
***********************************************************
*/
#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
vector<vector<int>> sudoku(9,vector<int>(9,-1));
vector<set<pair<int,int>>> blocks(9,set<pair<int,int>>());
int ans = 0;
void init(){
for(int i = 0; i < 9; i++){
cout << "\t ___ ___ ___ ___ ___ ___ ___ ___ ___\n\t|";
for(int j = 0; j < 9; j++){
cout << ""<< i+1<<","<<j+1 <<"" << "|";
}
cout<<"\n";
}
cout << "\t ___ ___ ___ ___ ___ ___ ___ ___ ___\n";
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if( i < 3 && j < 3) blocks[0].insert({i,j});
else if( i < 3 && j >= 3 && j <= 5) blocks[1].insert({i,j});
else if( i < 3 && j >5) blocks[2].insert({i,j});
else if( i >= 3 && i<= 5 && j < 3) blocks[3].insert({i,j});
else if( i >= 3 && i<= 5 && j >= 3 && j <= 5) blocks[4].insert({i,j});
else if( i >= 3 && i<= 5 && j > 5) blocks[5].insert({i,j});
else if( i > 5 && j < 3 ) blocks[6].insert({i,j});
else if( i > 5 && j >= 3 && j <= 5) blocks[7].insert({i,j});
else if( i > 5 && j > 5) blocks[8].insert({i,j});
}
}
}
pair<int,int> getNext(){
for(int i = 0 ; i < 9 ; i++){
for(int j = 0 ; j < 9 ; j++){
if(sudoku[i][j] == -1) {
return {i,j};
}
}
}
return {-1,-1};
}
void display(){
for(int i = 0; i < sudoku.size(); i++){
cout << "\t _ _ _ _ _ _ _ _ _\n\t|";
for(int j = 0; j < sudoku.size(); j++){
if(sudoku[i][j] == -1)
cout << " |";
else
cout << sudoku[i][j] << "|";
}
cout<<"\n";
}
cout << "\t _ _ _ _ _ _ _ _ _\n";
}
bool error(pair<int,int> pos, int num){
for(int k = 0; k < 9; k++){
if(pos.second != k && sudoku[pos.first][k] == num) return false;
if(pos.first != k && sudoku[k][pos.second] == num) return false;
}
int block_number = (pos.first /3) *3 + pos.second/3;
for(auto x: blocks[block_number]){
if(pos.first!=x.first && pos.second != x.second && num == sudoku[x.first][x.second]){
return false;
}
}
return true;
}
bool solve(){
ans++;
auto u = getNext();
if(u.first == -1 && u.second == -1) return true;
for(int k = 1; k <= 9; k++){
if(error(u,k)){
sudoku[u.first][u.second] = k;
// if(u.first == 0 && u.second == 0) display();
if(solve()) {
return true;
}
sudoku[u.first][u.second] = -1;
}
}
sudoku[u.first][u.second] = -1;
// cout << u.first <<" "<< u.second <<" Why is it not solveable\n";
return false;
}
int main()
{
fast_io;
init();
int N;
cin >> N;
for(int i = 0; i < N; i++){
int x,y,k;
cin >> x >> y >> k;
sudoku[x-1][y-1] = k;
}
display();
getchar();
cout << solve()<<" ";
display();
cout << ans <<" ";
}