-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
268 lines (211 loc) · 6.97 KB
/
Source.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "color.h"
/* Term Project for SE420 - 2015-2016 Spring
Kazım Gökberk Nur
20100601028
// Color.h library I found it from internet also, I used for painting cout function
CSP - Backtracking Method Implementation, Sudoku solving
This alghorithm implemented via using this source: https://see.stanford.edu/materials/icspacs106b/H19-RecBacktrackExamples.pdf
Logic behind this (backtracking):
- Find row, col of an unassigned cell
- If there is none, return true
- For digits from 1 to 9
- a) If there is no conflict for digit at row,col
- assign digit to row,col and recursively try fill in rest of grid
- b) If recursion successful, return true
- c) Else, remove digit and try another
- If all digits have been tried and nothing worked, return false
*/
// Defining size of sudoku, as we know sudoku game is 9x9 game
#define Size 9
using namespace std;
// This function finds an entry in Sudoku that is still unassigned
bool FindEmptySpot(int Sudoku[Size][Size], int &row, int &col);
// check if there are 0's
bool IsOkay(int Sudoku[Size][Size], int row, int col, int num);
// send to our prepared sudoku problem and try to assign values to available places (zeros (0's))
bool SolveSudoku(int Sudoku[Size][Size])
{
int row, col;
//If there are no "0"'s in the array return true, and problem solved yaaaaaaay!
if (FindEmptySpot(Sudoku, row, col) == false)
return true; // success!
// cout << "Iam row\t =" << row << " \tI am col \t =" << col;
// valid numbers are 1 to 9
for (int num = 1; num <= 9; num++)
{
// if it is okay
if (IsOkay(Sudoku, row, col, num) == true)
{
// assign it temporary
Sudoku[row][col] = num;
// return, if success, yay!
if (SolveSudoku(Sudoku) == true)
return true;
//solution is not possible set that value to 0 and call the function again
Sudoku[row][col] = 0;
}
}
return false; // recursive, backtracking
}
/* Searches the Sudoku to find an entry that is still unassigned. If
found, the reference parameters row, col will be set the location
that is unassigned, and true is returned. If no unassigned entries
remain, false is returned. */
//Finds empty spots (zero(0)'s) and if finds, ref parameters
bool FindEmptySpot(int Sudoku[Size][Size], int &row, int &col)
{
for (row = 0; row < Size; row++)
for (col = 0; col < Size; col++)
if (Sudoku[row][col] == 0)
return true;
return false;
}
/* Returns a boolean which indicates whether any assigned entry
in the specified row matches the given number. */
bool UsedInRow(int Sudoku[Size][Size], int row, int num)
{
for (int col = 0; col < Size; col++)
if (Sudoku[row][col] == num)
return true;
return false;
}
/* Returns a boolean which indicates whether any assigned entry
in the specified column matches the given number. */
bool UsedInCol(int Sudoku[Size][Size], int col, int num)
{
for (int row = 0; row < Size; row++)
if (Sudoku[row][col] == num)
return true;
return false;
}
/* Returns a boolean which indicates whether any assigned entry
within the specified 3x3 box matches the given number. */
bool UsedInBox(int Sudoku[Size][Size], int boxStartRow, int boxStartCol, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (Sudoku[row + boxStartRow][col + boxStartCol] == num)
return true;
return false;
}
/* Returns a boolean which indicates whether it will be legal to assign
num to the given row,col location. */
bool IsOkay(int Sudoku[Size][Size], int row, int col, int num)
{
// Checks if that number already exist in current row and it's column and inside the it's 3x3 box
return !UsedInRow(Sudoku, row, num) &&
!UsedInCol(Sudoku, col, num) &&
!UsedInBox(Sudoku, row - row % 3, col - col % 3, num);
}
/* A utility function to print Sudoku */
void Show(int Sudoku[Size][Size])
{
cout << "*****\t SOLUTION \t *****\n";
for (int row = 0; row < Size; row++)
{
if (row == 3 || row == 6) {
printf("\n");
}
for (int col = 0; col < Size; col++) {
if (col == 3 || col == 6) {
printf("\t");
}
printf("%2d", Sudoku[row][col]);
}
printf("\n");
}
}
//Showing which position we are entering, selected position painted with red on the line 173
// on line 177 we are coloring other positions with white.
void ShowInput(int Sudoku[Size][Size], int a, int b)
{
for (int row = 0; row < Size; row++)
{
if (row == 3 || row == 6) {
printf("\n");
}
for (int col = 0; col < Size; col++) {
if (col == 3 || col == 6) {
printf("\t");
}
if (row == a && col == b) {
//Painting red to selected value
cout << red;
printf("%2d", Sudoku[row][col]);
//resetting color (default is white
cout << white;
}
else {
printf("%2d", Sudoku[row][col]);
}
}
printf("\n");
}
}
int main()
{
// 0 represents empty location
// This is sample sudoku you can solve it using these lines:
/* if (SolveSudoku(Sudoku) == true)
Show(Sudoku);
else
printf("Solution is not possible check it again");
system("PAUSE");
*/
int Sudoku[Size][Size] = {
{ 1, 6, 4, 0, 0, 0, 0, 0, 2 },
{ 2, 0, 0, 4, 0, 3, 9, 1, 0 },
{ 0, 0, 5, 0, 8, 0, 4, 0, 7 },
{ 0, 9, 0, 0, 0, 6, 5, 0, 0 },
{ 5, 0, 0, 1, 0, 2, 0, 0, 8 },
{ 0, 0, 8, 9, 0, 0, 0, 3, 0 },
{ 8, 0, 9, 0, 4, 0, 2, 0, 0 },
{ 0, 7, 3, 5, 0, 9, 0, 0, 1 },
{ 4, 0, 0, 0, 0, 0, 6, 7, 9 }
};
//Initialize empty sudoku. full of zero's
int a[9][9] = { 0 };
//Start filling sudoku, for empty spaces enter zero (0)
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
int temp = a[i][j];
//Info texts
cout << "If you are done with entering puzzle enter -1\n";
cout << "Enter " << i + 1 << ".Row " << j + 1 << ".Column of sudoku\n";
//ShowInput function is for painting cout function. This is for location which position you're entering.
ShowInput(a, i, j);
//Ask number
cin >> temp;
//Number has to be between 1 and zero
if (temp > 9 || temp < -1) {
cout << "******** \t Enter a valid number \t ********* \n";
//IF users enters invalid value, go back to loop and do "j-1" because we don't want to fill that position with invalid number
j--;
continue;
}
//If user enters -1 call solve function
if (temp == -1) {
if (SolveSudoku(a) == true){
Show(a);
break;
}
else {
//If solution not possibble this will be printed
printf("Solution is not possible check it again");
system("PAUSE");
}
}
else {
//IF user enters a valid number assign it to selected position in the array
a[i][j] = temp;
}
//this is for clearing screen, makes the cmd screen more cool and pretty :)
system("cls");
}
}
system("PAUSE");
return 0;
}