-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
208 lines (172 loc) · 4.5 KB
/
common.h
File metadata and controls
208 lines (172 loc) · 4.5 KB
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
#pragma once
#include "mask.h"
#include <ostream>
#include <cassert>
#include <array>
const int N = 8;
const int INF = 0x7f7f7f7f;
const int DIR = 8;
const int dx[DIR] = {0, -1,-1,-1, 0, 1, 1, 1};
const int dy[DIR] = {-1,-1, 0, 1, 1, 1, 0,-1};
inline int getdir(const int curdx, const int curdy) {
for (int dir = 0; dir < DIR; ++dir) {
if (dx[dir] == curdx && dy[dir] == curdy) {
return dir;
}
}
throw std::runtime_error("dir not found");
}
inline int index(const int x, const int y) {
return x * N + y;
}
using MaskArray = std::array<std::array<int, N>, N>;
const MaskArray isborder{{
{0,1,0,0, 0,0,1,0},
{1,1,0,0, 0,0,1,1},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{1,1,0,0, 0,0,1,1},
{0,1,0,0, 0,0,1,0},
}};
const MaskArray iscorner{{
{1,0,0,0, 0,0,0,1},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0},
{1,0,0,0, 0,0,0,1},
}};
MaskArray toArray(const Mask& mask) {
MaskArray array;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
array[i][j] = mask.getbit(index(i, j));
}
}
return array;
}
struct Point {
int x, y;
Point() : x{-1}, y{-1} {}
Point(int x_, int y_) : x{x_}, y{y_} {}
int index() const {
return ::index(x, y);
}
bool operator == (const Point& ot) const {
return x == ot.x && y == ot.y;
}
bool operator != (const Point& ot) const {
return !(*this == ot);
}
Point operator ^ (const int dir) const {
Point p = *this;
p ^= dir;
return p;
}
Point& operator ^= (const int dir) {
assert(dir >= 0 && dir < DIR);
x += dx[dir];
y += dy[dir];
return *this;
}
friend std::ostream& operator <<(std::ostream& out, const Point& obj) {
return out << static_cast<char>(obj.y + 'A') << obj.x + 1;
}
bool operator < (const Point& p) const {
if (x != p.x) return x < p.x;
return y < p.y;
}
};
int DC[N][N];
Point D[N][N][8];
bool inside(const Point& p) {
return p.x >= 0 && p.y >= 0 && p.x < N && p.y < N;
}
void checkinside(const Point& p) {
if (!inside(p))
throw std::runtime_error("point must be inside the world");
}
const int MMASK = (1 << N) - 1;
std::array<Point, N*N> points;
Mask around[N*N];
Mask cornerMask;
Mask borderMask;
Mask lineMask1[N][N][DIR];
Mask lineMask2[N][N][N*N];
Mask aroundMask[N][1 << N];
void initPoints() {
for (int i=0; i<int(points.size()); ++i) {
points[i].x = i / N;
points[i].y = i % N;
}
}
void initAround() {
for (int i = 0; i < int(points.size()); ++i) {
const Point& cur = points[i];
for (int dir=0; dir<DIR; ++dir) {
const Point newp = cur ^ dir;
if (inside(newp)) {
around[i].setbit(newp.index());
}
}
}
}
void initCornerMask() {
for (int i=0; i<N; ++i) {
for (int u=0; u<N; ++u) {
if (iscorner[i][u])
cornerMask.setbit(index(i, u));
if (isborder[i][u])
borderMask.setbit(index(i, u));
}
}
}
void initD() {
for (const Point& cur : points) {
for (int dir=0; dir<DIR; ++dir) {
const Point newp = cur ^ dir;
if (inside(newp)) {
D[cur.x][cur.y][DC[cur.x][cur.y]] = Point{dx[dir], dy[dir]};
++DC[cur.x][cur.y];
}
}
}
}
void initLineMasks() {
for (const Point& cur : points) {
for (int dir=0; dir<DIR; ++dir) {
Point newp = cur;
for (;;) {
newp ^= dir;
if (!inside(newp)) break;
const int ind = newp.index();
lineMask2[cur.x][cur.y][ind] = lineMask1[cur.x][cur.y][dir];
lineMask1[cur.x][cur.y][dir].togglebit(ind, 0);
}
}
}
}
void initAroundMask() {
for (int row=0; row<N; ++row) {
for (int mask = 0; mask < 1 << N; ++mask) {
for (int bit=0; bit<N; ++bit) {
if (mask & (1 << bit)) {
aroundMask[row][mask] |= around[index(row, bit)];
aroundMask[row][mask].setbit(index(row, bit));
}
}
}
}
}
void init() {
initPoints();
initAround();
initCornerMask();
initD();
initLineMasks();
initAroundMask();
}