-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
222 lines (201 loc) · 7.37 KB
/
board.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
/////////////////////////////////////////////////////////////////////////////////////
// Robert Wagner
// CISC 3410 Assignment #2
// 2016-10-02
// board.cpp
////////////////////////////////////////////////////////////////////////////////////
#include "board.hpp"
namespace Sudoku {
std::map<std::string, int> UserFlag;
Board::Board() {
std::fill_n(values, SIZE, 0);
std::fill_n(degrees, SIZE, 0);
for (auto i = 0; i < SIZE; i++) domains[i] = ONES;
unsolved = SolvedSet(0).flip();
} // Board plain constructor
Board::Board(const Board & other) : unsolved(other.unsolved) {
for (auto i = 0; i < SIZE; i++) {
domains[i] = other.domains[i];
degrees[i] = other.degrees[i];
values[i] = other.values[i];
}
} // Board copy constructor
ValueVec Neighbors::getNeighbors(const DataType x) {
DataType row = RowOf[x];
DataType col = ColOf[x];
DataType j;
ValueVec temp; // up to 20 neighbors in 9x9 sudoku
temp.reserve(20);
for (auto i = 0; i < DIM; i++) {
j = i + row * DIM;
if (j != x) temp.push_back(j);
j = col + i * DIM;
if (j != x) temp.push_back(j);
}
// after adding row & column neighbors, there are only 4 more in the block to add
DataType r1 = 0, r2 = 0, c1 = 0, c2 = 0;
switch (col % BLOCKSIZE) {
case 0: c1 = col + 1; c2 = col + 2; break;
case 1: c1 = col - 1; c2 = col + 1; break;
case 2: c1 = col - 2; c2 = col - 1; break;
}
switch (row % BLOCKSIZE) {
case 0: r1 = row + 1; r2 = row + 2; break;
case 1: r1 = row - 1; r2 = row + 1; break;
case 2: r1 = row - 2; r2 = row - 1; break;
}
temp.push_back(c1 + DIM * r1);
temp.push_back(c1 + DIM * r2);
temp.push_back(c2 + DIM * r1);
temp.push_back(c2 + DIM * r2);
return temp;
} // Board::neighbors()
void Board::buildAllConstraints() {
constraints.clear();
std::fill_n(degrees, SIZE, 0);
for (auto from = 0; from < SIZE; from++) {
if (unsolved[from])
for (auto const &to : neighbors(from)) {
constraints.push_back(Constraint(from, to));
degrees[from]++;
}
}
} // Board::buildAllConstraints()
void Board::buildConstraints(const DataType from) {
constraints.clear();
for (auto const &to : neighbors(from)) {
constraints.push_back(Constraint(from, to));
}
} // Board::buildConstraints(from)
bool Board::clearSolved() {
bool changed = false;
for (auto i = 0; i < SIZE; i++) {
if (unsolved[i] && domains[i].count() == 1) {
unsolved[i] = false;
DataType j = 0;
while ((domains[i])[j] == false) j++;
values[i] = j + 1;
changed = true;
}
}
return changed;
} // Board::clearSolved()
bool Board::trimDomains() {
bool changed = false;
for (auto i = 0; i < SIZE; i++)
if (!unsolved[i])
for (auto const &j : neighbors(i)) {
(domains[j])[values[i]-1] = false;
changed = true;
}
return changed;
} // Board::trimDomains()
bool Board::reviseDomain(const DataType x, const DataType y) {
bool revised = false;
DomainSet val;
for (auto i = 0; i < DIM; i++)
if ((domains[x])[i]) {
val = domains[y];
val[i] = false;
if (val.none()) {
revised = true;
(domains[x])[i] = false;
}
}
return revised;
} // Board::reviseDomain()
DataType Board::chooseUnassigned() const {
// perform a radix sort of unassigned cells
ValueVec buckets[DIM + 1];
for (auto i = 0; i < SIZE; i++)
if (values[i] == 0) {
buckets[domains[i].count()].push_back(i);
}
DataType minRV = 0;
while (minRV < DIM && buckets[minRV].size() == 0) minRV++;
DataType maxDegrees = -1;
DataType maxCell = 0;
for (auto const &m : buckets[minRV]) {
if (degrees[m] > maxDegrees) {
maxCell = m;
maxDegrees = degrees[m];
}
}
return maxCell;
} // Board::chooseUnassigned()
bool Board::performAC3() {
while (!constraints.empty()) {
Constraint c = constraints.front();
constraints.pop_front();
degrees[c.second]--;
if (reviseDomain(c.first, c.second)) {
if ((domains[c.first]).none()) return false;
for (auto const &i : neighbors(c.first)) {
constraints.push_back(Constraint(i, c.first));
degrees[i]++;
}
}
}
return true;
} // Board::performAC3()
const ValueVec Board::getOrderedDomain(const DataType index) const {
ValueVec temp;
temp.reserve(9);
for (auto i = 0; i < DIM; i++)
if ((domains[index])[i])
temp.push_back(i + 1);
return temp;
} // Board::getOrderedDomain()
bool Board::operator()(const DataType index, const DataType val) {
if (val == 0) {
domains[index] = ONES;
unsolved[index] = true;
return true;
}
if((domains[index])[val - 1] == false) return false;
domains[index] &= SINGLES[val - 1];
for (auto const &n : neighbors(index)) {
(domains[n])[val - 1]=false;
if (domains[n].none()) return false;
}
values[index] = val;
unsolved[index] = false;
return true;
} // Board::operator()()
std::ostream& pretty(std::ostream& out) {
if (UserFlag.find("BoardFormat") == UserFlag.end())
UserFlag["BoardFormat"] = out.xalloc();
out.iword(UserFlag["BoardFormat"]) = PRETTY;
return (out);
}
std::ostream& plain(std::ostream& out) {
if (UserFlag.find("BoardFormat") == UserFlag.end())
UserFlag["BoardFormat"] = out.xalloc();
out.iword(UserFlag["BoardFormat"]) = PLAIN;
return (out);
}
std::ostream& count(std::ostream& out) {
if (UserFlag.find("BoardCount") == UserFlag.end())
UserFlag["BoardCount"] = out.xalloc();
out.iword(UserFlag["BoardCount"]) = COUNT;
return (out);
}
std::ostream& operator<<(std::ostream& out, const Board &b) {
if (out.iword(UserFlag["BoardFormat"]) == PRETTY) {
for (auto i = 0; i < SIZE; i++) {
if (i % (DIM * BLOCKSIZE) == 0)
out << fmtHLine;
if (i % DIM == 0) out << "|";
if (0 == b.values[i]) out << " .";
else out << " " << (int)(b.values[i]);
if ((i+1) % BLOCKSIZE == 0) out << " |";
if ((i+1) % DIM == 0) out << std::endl;
}
out << fmtHLine;
} else {
for (auto i = 0; i < SIZE; i++) out << (int)(b.values[i]);
out << std::endl;
}
return (out);
} // operator<<
} // namespace Sudoku