-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.hpp
182 lines (142 loc) · 3.4 KB
/
state.hpp
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
#pragma once
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
class State
{
private:
using u8 = std::uint8_t;
using u16 = std::uint16_t;
std::array<u16, 81> mCells;
public:
State()
{
// Set all flags to true
for (auto& c : mCells)
{
c = 511;
}
}
u16 GetCell(u16 cellNo) const
{
return mCells[cellNo];
}
void SetCell(u16 cellNo, u16 state)
{
mCells[cellNo] = state;
}
u8 PickNonFinalisedCell()
{
u8 bestCellNo = 255;
u8 bestPossibilities = 255;
for (u8 i = 0u; i != 81u; ++i)
{
u16 cell = GetCell(i);
u8 possibilities = NumPossibilities(cell);
// Shortcut: can't do better than 2
if (possibilities == u8(2))
{
return i;
}
if (possibilities > u8(1) && possibilities < bestPossibilities)
{
bestCellNo = i;
bestPossibilities = possibilities;
}
}
return bestCellNo;
}
// TODO: there are other ways to do this, investigate their #performance
static u8 NumPossibilities(u16 cell)
{
return __builtin_popcount(cell);
}
static u8 CellToNum(u16 cell)
{
if (cell == 0 || (cell & (cell - u16(1))) != 0)
{
return 0;
}
u8 n = 9;
u16 nn = 1;
while (nn != cell)
{
nn <<= 1;
--n;
}
return n;
}
static u16 NumToCell(u8 n)
{
if (u8(1) <= n && n <= u8(9))
{
--n;
return u16(1) << (u8(8) - n);
}
assert(n == 0);
return 511;
}
std::ostream& PrettyPrint(std::ostream& os) const
{
std::array<std::array<char, 41>, 41> grid;
for (auto& r : grid)
{
for (auto& c : r)
c = ':';
}
for (u8 i = 0u; i != 81u; ++i)
{
int row = i / 9;
int col = i % 9;
int r = 4 * row + 1;
if (row >= 3)
r += 2;
if (row >= 6)
r += 2;
int c = 4 * col + 1;
if (col >= 3)
c += 2;
if (col >= 6)
c += 2;
u16 cell = GetCell(i);
for (u8 k = 0u; k != 9; ++k)
{
int ki = k / 3;
int kj = k % 3;
grid[r + ki][c + kj] = ((cell & (1 << k)) ? char('1' + k) : ' ');
}
}
for (auto& r : grid)
{
for (auto& c : r)
os << c;
os << std::endl;
}
os << std::endl << std::endl << std::endl;
return os;
}
friend std::ostream& operator<<(std::ostream& os, const State& state)
{
for (u8 i = 0u; i != 81u; ++i)
{
os << int(State::CellToNum(state.GetCell(i)));
}
return os;
}
friend std::istream& operator>>(std::istream& is, State& state)
{
for (u8 i = 0u; i != 81u; ++i)
{
u8 n;
is >> n;
if (!(u8('0') <= n && n <= u8('9')))
{
// Invalid
return is;
}
state.SetCell(i, State::NumToCell(n - u8('0')));
}
return is;
}
};