This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell_functions.cpp
180 lines (167 loc) · 5.57 KB
/
cell_functions.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
#include <iostream>
#include <string>
#include <bitset>
#include <array>
#include <cmath>
#include "cell_functions.h"
using namespace std;
/*
* Gets sector number based on coordinates. Sector layout is below:
*
* -------------
* | 0 | 1 | 2 |
* -------------
* | 3 | 4 | 5 |
* -------------
* | 6 | 7 | 8 |
* -------------
*
* Each sector is 3x3, where coordinate (0, 0) is in the top left
*/
int get_sector(int x, int y) {
if (x >= 0 && x <= 2) {
if (y >= 0 && y <= 2) { return 0; }
else if (y >= 3 && y <= 5) { return 1; }
else if (y >= 6 && y <= 8) { return 2; }
}
else if (x >= 3 && x <= 5) {
if (y >= 0 && y <= 2) { return 3; }
else if (y >= 3 && y <= 5) { return 4; }
else if (y >= 6 && y <= 8) { return 5; }
}
else if (x >= 6 && x <= 8) {
if (y >= 0 && y <= 2) { return 6; }
else if (y >= 3 && y <= 5) { return 7; }
else if (y >= 6 && y <= 8) { return 8; }
}
} /* get_sector() */
/*
* Returns an array of coordinates for the requested sector.
*/
array<array<int, 2>, 9> get_sector_coords(int sector) {
switch(sector) {
case 0:
return {{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}};
break;
case 1:
return {{{0, 3}, {0, 4}, {0, 5}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}}};
break;
case 2:
return {{{0, 6}, {0, 7}, {0, 8}, {1, 6}, {1, 7}, {1, 8}, {2, 6}, {2, 7}, {2, 8}}};
break;
case 3:
return {{{3, 0}, {3, 1}, {3, 2}, {4, 0}, {4, 1}, {4, 2}, {5, 0}, {5, 1}, {5, 2}}};
break;
case 4:
return {{{3, 3}, {3, 4}, {3, 5}, {4, 3}, {4, 4}, {4, 5}, {5, 3}, {5, 4}, {5, 5}}};
break;
case 5:
return {{{3, 6}, {3, 7}, {3, 8}, {4, 6}, {4, 7}, {4, 8}, {5, 6}, {5, 7}, {5, 8}}};
break;
case 6:
return {{{6, 0}, {6, 1}, {6, 2}, {7, 0}, {7, 1}, {7, 2}, {8, 0}, {8, 1}, {8, 2}}};
break;
case 7:
return {{{6, 3}, {6, 4}, {6, 5}, {7, 3}, {7, 4}, {7, 5}, {8, 3}, {8, 4}, {8, 5}}};
break;
case 8:
return {{{6, 6}, {6, 7}, {6, 8}, {7, 6}, {7, 7}, {7, 8}, {8, 6}, {8, 7}, {8, 8}}};
break;
default:
cout << "Error! Sector coordinates must be between 0-8, inclusive." << endl;
exit(EXIT_FAILURE);
}
} /* get_sector_coords() */
/*
* Finds all possible combinations of groups of 'n' with the coordinates list
* passed in.
*
* Where 'x' is the number of combinations possible with 'n' list size and
* 'k' group size:
*
* x = n! / ( k! * (n - k)! )
*/
void combination_finder(array< array<int, 2>, 9> coords, int n, int offset,
vector< vector< array<int, 2> > > &combinations,
vector< array<int, 2> > &combination) {
if (n == 0) {
combinations.push_back(combination);
return;
}
for (int i = offset; i <= 9 - n; ++i) {
combination.push_back(coords[i]);
combination_finder(coords, n - 1, i + 1, combinations, combination);
combination.pop_back();
}
} /* combination_finder() */
/*
* Removes a value from every Cell's 'candidates' bitset in row x and column y.
* No error checking - DANGEROUS!
*/
void remove_candidate_row_col(array< array<Cell, 9>, 9>&board, int x, int y, int candidate) {
for (int i = 0; i < 9; i++) {
board[x][i].candidates.set((candidate - 1), 0);
board[i][y].candidates.set((candidate - 1), 0);
}
} /* remove_candidate_row_col() */
/*
* Removes a value from every Cell's 'candidates' bitset in row x
*/
void remove_candidate_row(array< array<Cell, 9>, 9> &board, int x, int y, int candidate) {
for (int i = 0; i < 9; i++) {
//if (i != y) {
board[x][i].candidates.set((candidate - 1), 0);
//}
}
} /* remove_candidate_row() */
/*
* Removes a value from every Cell's 'candidates' bitset in column y
*/
void remove_candidate_col(array< array<Cell, 9>, 9> &board, int x, int y, int candidate) {
for (int i = 0; i < 9; i++) {
//if (i != x) {
board[i][y].candidates.set((candidate - 1), 0);
//}
}
} /* remove_candidate_col() */
/*
* Adds a value to the sector's bitset, where the sector is determined by the
* coordinates passed in.
*/
void add_value_sector(int x, int y, int value) {
sectors[get_sector(x, y)].set((value - 1), 1);
} /* add_value_sector() */
/*
* Removes a value 'candidate' from every cell in a sector, where the sector
* is determined by the coordinates passed in.
*/
void remove_candidate_sector(array< array<Cell, 9>, 9>&board, int x, int y, int candidate) {
array<array<int, 2>, 9> coords = get_sector_coords(get_sector(x, y));
int a = 0;
int b = 0;
for (int i = 0; i < 9; i++) {
a = coords[i][0];
b = coords[i][1];
//if ((a != x) && (b != y)) {
board[a][b].candidates.set((candidate - 1), 0);
//}
}
} /* remove_candidate_sector() */
/*
* Given a cell, performs (log2(candidates) + 1), translating the single set
* candidate bit into the solved number. Also resets candidate bits for easier
* use later. Makes sure that the solved value is removed from candidacy from
* the Cell's row, column, and sector.
*
*/
void solve_cell(array< array<Cell, 9>, 9>&board, int x, int y) {
// extract value from bitset
board[x][y].val = log2(board[x][y].candidates.to_ulong()) + 1;
cout << "Solved (" << x << ", " << y << ") with the value --" << board[x][y].val << "--" << endl;
cout << "-----------------------------------------------------" << endl;
// reset bits to 0 to avoid confusion later
board[x][y].candidates.reset();
remove_candidate_row_col(board, x, y, board[x][y].val);
remove_candidate_sector(board, x, y, board[x][y].val);
add_value_sector(x, y, board[x][y].val);
} /* solve_cell() */