-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku_Solver.cpp
223 lines (221 loc) · 5.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
class SudokuGrid{
char a[9][9];
public:SudokuGrid(){
optionMenu();
}
public:void optionMenu(){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
a[i][j]='.';
}
}
int choice;
cout<<"1. Generate the Puzzle"<<"\n";
cout<<"2. Manually Input the Puzzle"<<"\n";
cin>>choice;
if(choice==1){
SudokuGenerator();
}
else if(choice==2){
char ch;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cout<<"Enter Value for Cell ["<<i+1<<"]["<<j+1<<"]"<<"\n";
cin>>ch;
a[i][j]=ch;
}
}
}
}
public:void setCellValue(int row, int col,char key){
a[row][col]=key;
}
public:int getCellValue(int row, int col){
int cellValue=a[row][col];
return cellValue;
}
public:void DisplayGrid(){
cout<<"-------------------------\n";
for(int i=0;i<9;i++){
cout<<"| ";
for(int j=0;j<9;j++){
cout<<a[i][j]<<" ";
if(j%3==2){
cout<<"| ";
}
}
if(i%3==2){
cout<<"\n";
cout<<"-------------------------";
}
cout<<"\n";
}
}
// Returns false if given 3 x 3 block contains num.
bool unUsedInBox(int row, int col, int num)
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (a[i+row][j+col]==num+'0') {
return false;
}
}
}
return true;
}
// Random generator
int randomGenerator(int num){
return (int)floor(
(float)(rand() / double(RAND_MAX) * num + 1));
}
// Fill a 3 x 3 matrix.
void fillBox(int row, int col){
int num;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
do {
num = randomGenerator(9);
} while (!unUsedInBox(row, col, num));
a[row+i][col+j]=num+'0';
}
}
}
// Fill the diagonal SRN number of SRN x SRN matrices
void fillDiagonal(){
for (int i = 0; i < 9; i = i + 3){
// for diagonal box, start coordinates->i==j
fillBox(i,i);
}
}
void SudokuGenerator(){
fillDiagonal();
SSolver();
ll k=0;
while(k<55){
ll x=rand()%9;
ll y=rand()%9;
if(a[x][y]!='.'){
a[x][y]='.';
k++;
}
}
}
private:bool Check(int i,int j,char x){
// Check Row
for(int k=0;k<9;k++){
if(a[i][k]==x){
return false;
}
}
// Check Column
for(int k=0;k<9;k++){
if(a[k][j]==x){
return false;
}
}
// Check Individual Square
ll i1=i/3;
ll j1=j/3;
for(int k=i1*3;k<i1+3;k++){
for(int l=j1*3;l<j1+3;l++){
if(a[k][l]==x){
return false;
}
}
}
return true;
}
private:bool SSolver(){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(a[i][j]=='.'){
for(int k='1';k<='9';k++){
if(Check(i,j,k)){
a[i][j]=k;
if(SSolver()){
return true;
}
else{
a[i][j]='.';
}
}
}
return false;
}
}
}
return true;
}
};
class SudokuSolver{
SudokuGrid a;
public: SudokuSolver(){
a.DisplayGrid();
cout<<"\n\nCalculating possibilities...\n";
cout<<"Backtracking across puzzle....\n";
cout<<"Validating cells and values...\n\n";
SolveCheck();
}
private:bool Check(int i,int j,char x){
// Check Row
for(int k=0;k<9;k++){
if(a.getCellValue(i,k)==x){
return false;
}
}
// Check Column
for(int k=0;k<9;k++){
if(a.getCellValue(k,j)==x){
return false;
}
}
// Check Individual Square
ll i1=i/3;
ll j1=j/3;
for(int k=i1*3;k<i1+3;k++){
for(int l=j1*3;l<j1+3;l++){
if(a.getCellValue(k,l)==x){
return false;
}
}
}
return true;
}
private:bool SSolver(){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(a.getCellValue(i,j)=='.'){
for(int k='1';k<='9';k++){
if(Check(i,j,k)){
a.setCellValue(i,j,k);
if(SSolver()){
return true;
}
else{
a.setCellValue(i,j,'.');
}
}
}
return false;
}
}
}
return true;
}
private: void SolveCheck(){
if(SSolver()){
cout<<"Successfully Solved!!! :)"<<"\n";
a.DisplayGrid();
}
else{
cout<<"Unsolvable Sudoku!!! :("<<"\n";
}
}
};
int main(){
SudokuSolver s1;
return 0;
}