-
Notifications
You must be signed in to change notification settings - Fork 1
/
staticData.hpp
94 lines (79 loc) · 2.02 KB
/
staticData.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
#pragma once
#include <array>
#include <bitset>
#include <cstdint>
std::array<std::array<uint8_t, 9>, 27> CreateGroups()
{
std::array<std::array<uint8_t, 9>, 27> ret;
// rows
for (int i = 0; i != 9; i++)
{
for (int j = 0; j != 9; j++)
{
ret[i][j] = 9 * i + j;
}
}
// columns
for (int i = 9; i != 18; i++)
{
for (int j = 0; j != 9; j++)
{
ret[i][j] = (i - 9) + 9 * j;
}
}
// blocks
for (int i = 18; i != 27; i++)
{
for (int j = 0; j != 9; j++)
{
int ni = i - 18;
ret[i][j] = 27 * (ni / 3) + 3 * (ni % 3) + 9 * (j / 3) + (j % 3);
}
}
return ret;
}
const std::array<std::array<uint8_t, 9>, 27>& GetGroups()
{
static const std::array<std::array<uint8_t, 9>, 27> groups = CreateGroups();
return groups;
}
std::uint32_t GetCellGroups(uint8_t cellNo)
{
return (
(1u << (cellNo / 9u)) |
(1u << (9u + cellNo % 9u)) |
(1u << (18u + 3u * (cellNo / 27u) + (cellNo % 9u) / 3u)));
}
std::array<std::array<std::uint8_t, 20>, 81> CreateCellNeighbours()
{
using u8 = std::uint8_t;
std::array<std::array<u8, 20>, 81> neighbours;
const auto& groups = GetGroups();
for (u8 i = 0u; i != 81u; ++i)
{
std::bitset<81> set;
for (u8 groupNo : (u8[]){u8(i / 9u), u8(9u + i % 9u), u8(18u + 3u * (i / 27u) + (i % 9u) / 3u)})
{
for (auto neighbour : groups[groupNo])
{
set.set(neighbour);
}
}
auto& nn = neighbours[i];
u8 k = 0u;
for (u8 j = 0u; j != 81u; j++)
{
if (set.test(j) && i != j)
{
nn[k] = j;
++k;
}
}
}
return neighbours;
}
const std::array<std::uint8_t, 20>& GetCellNeighbours(std::uint8_t cellNo)
{
static const std::array<std::array<std::uint8_t, 20>, 81> cellNeighbours = CreateCellNeighbours();
return cellNeighbours[cellNo];
}