-
Notifications
You must be signed in to change notification settings - Fork 1
/
Algorithm.cpp
234 lines (209 loc) · 5.44 KB
/
Algorithm.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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef vector<int> *Board;
// Generate a 9x9 board filled with 0
Board MakeBoard()
{
Board result = new vector<int>[9];
for (int i(0); i < 9; i++)
{
for (int j(0); j < 9; j++)
{
result[i].push_back(0);
}
}
return result;
}
// Check if the number filled at board[row][col] is valid according to Sudoku rule
bool IsValid(Board board, int row, int col, int num)
{
// Check Row
for (int i(0); i < 9; i++)
{
if (board[row][i] == num)
return false;
}
// Check Column
for (int i(0); i < 9; i++)
{
if (board[i][col] == num)
return false;
}
// Check box
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i(0); i < 3; i++)
{
for (int j(0); j < 3; j++)
{
if (board[startRow + i][startCol + j] == num)
return false;
}
}
return true;
}
// check if the current board is empty
bool IsEmpty(Board board)
{
for (int i(0); i < 9; i++)
{
for (int j(0); j < 9; j++)
{
if (board[i][j] == 0)
return true;
}
}
return false;
}
// Input: Emptry sudoku board filled with 0
// Output: A random Sudoku Board that is valid
bool GenerateBoard(Board board)
{
srand(time(NULL));
vector<int> numList;
for (int i(1); i <= 9; i++)
numList.push_back(i);
int row = 0;
int col = 0;
for (int i(0); i < 81; i++)
{
row = i / 9;
col = i % 9;
if (board[row][col] == 0)
{
random_shuffle(numList.begin(), numList.end());
for (int j(0); j < numList.size(); j++)
{
if (IsValid(board, row, col, numList[j]))
{
board[row][col] = numList[j];
if (!IsEmpty(board))
{
return true;
}
else
{
if (GenerateBoard(board))
return true;
}
}
}
break;
}
}
board[row][col] = 0;
return false;
}
//Return the copy of the current Board
Board CopyBoard(Board board)
{
Board result = new vector<int>[9];
for (int i(0); i < 9; i++)
{
for (int j(0); j < 9; j++)
{
result[i].push_back(board[i][j]);
}
}
return result;
}
//Input: Board and a vector of Board to record the solutions
//Output: Return the number of solutions a Board can have. Our aim is to have exactly one solutions
vector<Board> &TestUnique(Board board, vector<Board> &solutions)
{
int memorize = board[8][8];
if (solutions.size() < 2)
{
vector<int> numList;
for (int i(1); i <= 9; i++)
{
numList.push_back(i);
}
int row(0), col(0);
for (int i(0); i < 81; i++)
{
row = i / 9;
col = i % 9;
if (board[row][col] == 0)
{
for (int j(0); j < numList.size(); j++)
{
if (IsValid(board, row, col, numList[j]))
{
board[row][col] = numList[j];
if (!IsEmpty(board))
{
Board result = CopyBoard(board);
solutions.push_back(result);
}
solutions = TestUnique(board, solutions);
}
}
break;
}
}
board[row][col] = 0;
}
board[8][8] = memorize;
return solutions;
}
//Intput: number of cell needed to delete
//Output: A Sudoku puzzle has a unique solution with that number of cells deleted
Board GeneratePuzzle(int cell)
{
srand(time(NULL));
Board board = MakeBoard();
GenerateBoard(board);
vector<int> choices;
for (int i(0); i < 81; i++)
{
choices.push_back(i);
}
random_shuffle(choices.begin(), choices.end());
while (choices.size() > cell)
{
int index = choices.at(0);
choices.erase(choices.begin());
int num = board[index / 9][index % 9];
board[index / 9][index % 9] = 0;
vector<Board> solutions;
solutions = TestUnique(board, solutions);
if (solutions.size() >= 2)
{
choices.push_back(index);
board[index / 9][index % 9] = num;
}
}
return board;
}
Board EasyMode(){
srand(time(NULL));
int cells = rand() % 15 + 44;
Board board = GeneratePuzzle(cells);
return board;
}
Board NormalMode()
{
srand(time(NULL));
int cells = rand() % 10 + 34;
Board board = GeneratePuzzle(cells);
return board;
}
Board HardMode()
{
srand(time(NULL));
int cells = rand()% 6 + 27;
Board board = GeneratePuzzle(cells);
return board;
}
int Hint(Board solution, int row, int col){
row -= 1;
col -= 1;
if ((0 <= row && row <= 8) && (0 <= col && col <= 8))
return solution[row][col];
else
return -1;
}