-
Notifications
You must be signed in to change notification settings - Fork 2
/
lbdist.h
208 lines (168 loc) · 5.58 KB
/
lbdist.h
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
#pragma once
/*
Compute a rough lower bound on the number of additional moves needed to win given this position
Done using floodfills from each edge/corner for each side, only going in the 3 forward directions
Increase distance when crossing an opponent virtual connection?
Decrease distance when crossing your own virtual connection?
*/
#include "board.h"
#include "move.h"
class LBDists {
struct MoveDist {
Move pos;
int dist;
int dir;
MoveDist() { }
MoveDist(Move p, int d, int r) : pos(p), dist(d), dir(r) { }
MoveDist(int x, int y, int d, int r) : pos(Move(x,y)), dist(d), dir(r) { }
};
//a specialized priority queue
//distances must start at 0 or 1
//new values must be the same as current smallest, or larger but smaller than min + maxvals
class IntPQueue {
static const int maxvals = 4; //maximum number of distinct values that can be stored
MoveDist vals[maxvals][361];
int counts[maxvals];
int current; //which vector
int num; //int num elements total
public:
IntPQueue(){
reset();
}
void reset(){
current = 0;
num = 0;
for(int i = 0; i < maxvals; i++)
counts[i] = 0;
}
void push(const MoveDist & v){
int i = v.dist % maxvals;
vals[i][counts[i]++] = v;
num++;
}
bool pop(MoveDist & ret){
if(num == 0){
current = 0;
return false;
}
while(counts[current] == 0)
current = (current+1) % maxvals;
ret = vals[current][--counts[current]];
num--;
return true;
}
};
int dists[12][2][361]; //[edge/corner][player][cell]
static const int maxdist = 1000;
IntPQueue Q;
const Board * board;
int & dist(int edge, int player, int i) { return dists[edge][player-1][i]; }
int & dist(int edge, int player, const Move & m) { return dist(edge, player, board->xy(m)); }
int & dist(int edge, int player, int x, int y) { return dist(edge, player, board->xy(x, y)); }
void init(int x, int y, int edge, int player, int dir){
int val = board->get(x, y);
if(val != 3 - player){
Q.push(MoveDist(x, y, (val == 0), dir));
dist(edge, player, x, y) = (val == 0);
}
}
public:
LBDists() : board(NULL) {}
LBDists(const Board * b) { run(b); }
void run(const Board * b, bool crossvcs = true, int side = 0) {
board = b;
for(int i = 0; i < 12; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < board->vecsize(); k++)
dists[i][j][k] = maxdist; //far far away!
int m = board->get_size()-1, e = board->get_size_d()-1;
int start, end;
if(side){ start = end = side; }
else { start = 1; end = 2; }
for(int player = start; player <= end; player++){
init(0, 0, 0, player, 3); flood(0, player, crossvcs); //corner 0
init(m, 0, 1, player, 4); flood(1, player, crossvcs); //corner 1
init(e, m, 2, player, 5); flood(2, player, crossvcs); //corner 2
init(e, e, 3, player, 0); flood(3, player, crossvcs); //corner 3
init(m, e, 4, player, 1); flood(4, player, crossvcs); //corner 4
init(0, m, 5, player, 2); flood(5, player, crossvcs); //corner 5
for(int x = 1; x < m; x++) { init(x, 0, 6, player, 3+(x==1)); } flood(6, player, crossvcs); //edge 0
for(int y = 1; y < m; y++) { init(m+y, y, 7, player, 4+(y==1)); } flood(7, player, crossvcs); //edge 1
for(int y = m+1; y < e; y++) { init(e, y, 8, player, 5+(y==m+1)); } flood(8, player, crossvcs); //edge 2
for(int x = m+1; x < e; x++) { init(x, e, 9, player, 0+(x==e-1)); } flood(9, player, crossvcs); //edge 3
for(int x = 1; x < m; x++) { init(x, m+x, 10, player, 1+(x==m-1)); } flood(10, player, crossvcs); //edge 4
for(int y = 1; y < m; y++) { init(0, y, 11, player, 2+(y==m-1)); } flood(11, player, crossvcs); //edge 5
}
}
void flood(int edge, int player, bool crossvcs){
int otherplayer = 3 - player;
MoveDist cur;
while(Q.pop(cur)){
for(int i = 5; i <= 7; i++){
int nd = (cur.dir + i) % 6;
MoveDist next(cur.pos + neighbours[nd], cur.dist, nd);
if(board->onboard(next.pos)){
int pos = board->xy(next.pos);
int colour = board->get(pos);
if(colour == otherplayer)
continue;
if(colour == 0){
if(!crossvcs && //forms a vc
board->get(cur.pos + neighbours[(nd - 1) % 6]) == otherplayer &&
board->get(cur.pos + neighbours[(nd + 1) % 6]) == otherplayer)
continue;
next.dist++;
}
if( dist(edge, player, pos) > next.dist){
dist(edge, player, pos) = next.dist;
if(next.dist < board->get_size())
Q.push(next);
}
}
}
}
}
int isdraw(){
int outcome = 0;
for(int y = 0; y < board->get_size_d(); y++){
for(int x = board->linestart(y); x < board->lineend(y); x++){
Move pos(x,y);
if(board->encirclable(pos, 1) || get(pos, 1) < maxdist-5)
outcome |= 1;
if(board->encirclable(pos, 2) || get(pos, 2) < maxdist-5)
outcome |= 2;
if(outcome == 3)
return -3;
}
}
return -outcome;
}
int get(Move pos){ return min(get(pos, 1), get(pos, 2)); }
int get(Move pos, int player){ return get(board->xy(pos), player); }
int get(int pos, int player){
int list[6];
for(int i = 0; i < 6; i++)
list[i] = dist(i, player, pos);
partialsort(list, 2);
int corners = list[0] + list[1] - 1;
for(int i = 6; i < 12; i++)
list[i-6] = dist(i, player, pos);
partialsort(list, 3);
int edges = list[0] + list[1] + list[2] - 2;
return min(corners, edges);
}
//partially sort the list with selection sort
void partialsort(int * list, int max){
for(int i = 0; i < max; i++){
int mini = i;
for(int j = i+1; j < 6; j++)
if(list[mini] > list[j])
mini = j;
if(mini != i){
int t = list[i];
list[i] = list[mini];
list[mini] = t;
}
}
}
};