-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
279 lines (219 loc) · 7.67 KB
/
main.cpp
File metadata and controls
279 lines (219 loc) · 7.67 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "mask.h"
#include "common.h"
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <stdexcept>
#include <fstream>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <chrono>
#include <cassert>
using namespace std;
class TimeLimit : public std::exception {};
Point newp;
struct World {
Mask empty;
Mask discs[2];
Mask frontier[2];
int free;
pair<uint64_t, uint64_t> hashpair() const {
return {discs[0].mask, discs[1].mask};
}
bool makeMove(const Point& p, const int myindex) {
const int enemyindex = 3 - myindex;
Mask totalMask;
for (int dir=0; dir<DIR; ++dir) {
const Mask myInDir = discs[myindex - 1] & lineMask1[p.x][p.y][dir];
if (myInDir.mask != 0) {
int closest = myInDir.closest(dir);
if ((empty & lineMask2[p.x][p.y][closest]).mask != 0) // this direction intersects an empty region
continue;
totalMask |= lineMask2[p.x][p.y][closest];
}
}
if (totalMask.mask == 0) return false; // cannot make a move
--free;
const int ind = index(p.x, p.y);
discs[enemyindex - 1] ^= totalMask;
discs[myindex - 1] ^= totalMask;
discs[myindex - 1].togglebit(ind, 0);
empty.togglebit(ind, 1);
updateFrontier();
return true;
}
void updateFrontier() {
frontier[0] = frontier[1] = Mask{};
for (int i=0; i<N; ++i) {
frontier[0] |= aroundMask[i][(discs[0].mask >> (N * i)) & MMASK];
frontier[1] |= aroundMask[i][(discs[1].mask >> (N * i)) & MMASK];
}
}
friend istream& operator >>(istream& in, World& obj) {
obj.free = N*N;
obj.frontier[0] = obj.frontier[1] = Mask{};
obj.discs[0] = obj.discs[1] = Mask{};
for (int i=0; i<N; ++i) {
for (int u=0; u<N; ++u) {
int cur;
in >> cur;
if (!in) {
return in;
}
if (cur == 3)
cur = 0;
if (cur != 0) {
--obj.free;
obj.discs[cur-1].setbit(index(i, u));
}
}
}
obj.empty = ~(obj.discs[0] | obj.discs[1]);
obj.updateFrontier();
return in;
}
friend ostream& operator <<(ostream& out, const World& obj) {
for (int i=0; i<N; ++i) {
for (int u=0; u<N; ++u) {
int value = 0;
const int ind = index(i, u);
if (obj.discs[0].getbit(ind) && obj.discs[1].getbit(ind))
throw runtime_error("both discs in same place");
if (obj.discs[0].getbit(ind))
value = 1;
if (obj.discs[1].getbit(ind))
value = 2;
out << value << ' ';
}
out << '\n';
}
return out;
}
};
int calcScore(const World& world, const int myindex, int deep, bool enemygo, bool nobodycanmove) {
int score = 0;
const int enemyindex = 3 - myindex;
int mydiscs = world.discs[myindex - 1].bitcount();
int enemydiscs = world.discs[enemyindex - 1].bitcount();
if (nobodycanmove) {
if (mydiscs > enemydiscs) return INF + mydiscs;
if (mydiscs < enemydiscs) return -INF - enemydiscs;
return 0;
}
Mask emptyFrontier = world.frontier[myindex - 1] & world.empty;
score -= emptyFrontier.bitcount();
// score for capturing the corner:
Mask corner = cornerMask & world.discs[myindex - 1];
score += corner.bitcount() * 100;
Mask contactEnemy = world.frontier[myindex - 1] & world.discs[enemyindex - 1];
score += contactEnemy.bitcount();
if (world.free >= 30)
score -= mydiscs / 4;
if (enemygo)
score -= calcScore(world, 3 - myindex, deep, false, false);
return score;
}
const std::chrono::milliseconds TIME_LIMIT{940};
bool check = false;
double elapsed() {
return static_cast<double>(clock()) / CLOCKS_PER_SEC;
}
int g_sum = 0;
std::chrono::system_clock::time_point start;
int firstdeep;
pair<Point, int> findBestMove(const World& world, const int myindex, const int deep, int alpha, int beta) {
Point bestMove;
int bestScore = -INF - 100;
if (check && std::chrono::system_clock::now() - start > TIME_LIMIT) throw TimeLimit{};
const int enemyindex = 3 - myindex;
vector<pair<int, Point>> moves;
Mask mask = world.frontier[enemyindex - 1] & world.empty;
for (; mask.mask != 0; ) {
//cerr << "mask:\n" << mask << endl;
const Point& cur = points[mask.removeRightMost()];
World w = world;
w.makeMove(cur, myindex);
moves.emplace_back(deep == firstdeep ? -calcScore(w, myindex, deep, true, false) : 0, cur);
}
sort(moves.begin(), moves.end());
for (const pair<int, Point>& m : moves) {
const Point& cur = m.second;
World w = world;
if (w.makeMove(cur, myindex)) {
//for (int i=0; i<4-deep; ++i)
//cerr << ' ';
//cerr << myindex << " made a move " << cur << endl;
int score = 0;
if (deep > 1) {
pair<Point, int> bestMove = findBestMove(w, 3 - myindex, deep - 1, -beta, -alpha);
if (bestMove.first.x == -1) {
// enemy could not make a move, however, the game is not finished yet
// I make another move
bestMove = findBestMove(w, myindex, deep - 1, alpha, beta);
if (bestMove.first.x == -1) {
// I cannot make a move too
// The game is over
score = calcScore(w, myindex, deep, true, true);
} else {
score = bestMove.second;
}
} else {
score = -bestMove.second;
}
} else {
score = calcScore(w, myindex, deep, true, false);
//score = -m.first;
}
if (score > bestScore) {
bestScore = score;
bestMove = cur;
}
alpha = max(alpha, score);
if (alpha >= beta)
break;
}
}
return {bestMove, bestScore};
}
int main(int argc, char** argv) {
init();
for (int turn = 0; ; ++turn) {
start = std::chrono::system_clock::now();
World world;
if (!(cin >> world)) {
break;
}
int myindex; cin >> myindex;
Point mov;
int score = -INF; cerr << "World:\n" << world << endl;
cerr << "free cells: " << world.free << endl;
int maxdepth = 0;
check = false;
for (int depth=6; ; ++depth) {
//for (int depth=3; depth<=3; ++depth) {
try {
firstdeep = depth;
pair<Point, int> bestMove = findBestMove(world, myindex, depth, -INF - 111, INF + 111);
mov = bestMove.first;
score = bestMove.second;
cerr << "depth: " << depth << ", move: " << bestMove.first << ", score: " << bestMove.second << endl;
check = true;
maxdepth = depth;
} catch (const TimeLimit& t) {
break;
}
}
if (mov.x == -1) {
cout << "pass" << endl;
} else {
cout << mov << endl;
}
cerr << "Score " << score << " at depth " << maxdepth << endl;
cerr << "Elapsed: " << elapsed() << endl;
}
}