-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
713 lines (619 loc) · 25.8 KB
/
main.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include <bits/stdc++.h>
struct PlayerInfo {
unsigned army;
unsigned land;
};
struct CellArmy {
unsigned owner;
unsigned size;
};
enum class ArmyType { Field, City, Capital };
enum class Hidden { Empty, Obstacle };
struct Mountain {
};
struct Armed {
ArmyType type;
CellArmy army;
};
using Visible = std::variant<Mountain, Armed>;
using Cell = std::variant<Hidden, Visible>;
using CellI = std::pair<unsigned, unsigned>;
template <> struct std::hash<CellI> {
std::size_t operator()(CellI const &x) const noexcept
{
return std::hash<unsigned>{}(x.first) ^
(std::hash<unsigned>{}(x.second) << 1);
};
};
struct Skip {
};
enum class MoveType { All = 1, Half = 2 };
struct Move {
MoveType type;
CellI src;
CellI dest;
};
using Turn = std::variant<Skip, Move>;
std::istream &operator>>(std::istream &in, PlayerInfo &x)
{
return (in >> x.army >> x.land);
};
std::istream &operator>>(std::istream &in, Hidden &x)
{
int t;
in >> t;
if (t == 1) {
x = Hidden::Empty;
} else if (t == 2) {
x = Hidden::Obstacle;
} else {
throw std::runtime_error("Invalid type for hidden cell");
}
return in;
}
std::istream &operator>>(std::istream &in, CellArmy &x)
{
in >> x.owner >> x.size;
return in;
}
std::istream &operator>>(std::istream &in, Visible &x)
{
int t;
in >> t;
if (t == 1 || t == 2 || t == 3) {
Armed a;
if (t == 1) {
a.type = ArmyType::Field;
} else if (t == 2) {
a.type = ArmyType::City;
} else if (t == 3) {
a.type = ArmyType::Capital;
}
in >> a.army;
x = a;
} else if (t == 4) {
x = Mountain{};
} else {
throw std::runtime_error("Invalid type for visible cell");
}
return in;
}
std::istream &operator>>(std::istream &in, Cell &c)
{
int visible;
in >> visible;
if (visible) {
Visible v;
in >> v;
c = v;
} else {
Hidden h;
in >> h;
c = h;
}
return in;
}
std::ostream &operator<<(std::ostream &out, const Skip &)
{
return (out << -1);
}
std::ostream &operator<<(std::ostream &out, const CellI &x)
{
return (out << (x.second + 1) << " " << (x.first + 1));
}
std::ostream &operator<<(std::ostream &out, const Move &x)
{
return (out << (int)x.type << " " << x.src << " " << x.dest);
}
std::ostream &operator<<(std::ostream &out, const Turn &x)
{
if (std::holds_alternative<Skip>(x)) {
out << std::get<Skip>(x) << std::endl;
} else if (std::holds_alternative<Move>(x)) {
out << std::get<Move>(x) << std::endl;
} else {
throw std::invalid_argument("Invalid turn");
}
return out;
}
std::optional<Armed> get_armed(const Cell &c) {
if (!std::holds_alternative<Visible>(c)) {
return std::nullopt;
}
const auto &v = std::get<Visible>(c);
if (!std::holds_alternative<Armed>(v)) {
return std::nullopt;
}
return std::get<Armed>(v);
}
bool can_pass(const Cell &c) {
if (std::holds_alternative<Hidden>(c)) {
return std::get<Hidden>(c) == Hidden::Empty;
} else if (std::holds_alternative<Visible>(c)) {
return std::holds_alternative<Armed>(std::get<Visible>(c));
} else {
return false;
}
}
struct Field {
unsigned size_x;
unsigned size_y;
std::vector<std::vector<Cell>> table;
Field(unsigned x, unsigned y) : size_x(x), size_y(y)
{
table.resize(x, std::vector<Cell>(y));
}
void read_table(std::istream &in)
{
for (unsigned y = 0; y < size_y; ++y) {
for (unsigned x = 0; x < size_x; ++x) {
in >> table[x][y];
}
}
}
void check(const CellI &pos) const
{
if (pos.first < 0 || pos.first >= size_x) {
throw std::out_of_range("Field pos out of range\n");
}
if (pos.second < 0 || pos.second >= size_y) {
throw std::out_of_range("Field pos out of range\n");
}
}
Cell at(const CellI &pos) const
{
check(pos);
return table[pos.first][pos.second];
}
Cell &at(const CellI &pos)
{
check(pos);
return table[pos.first][pos.second];
};
std::vector<CellI> neighbors(const CellI &pos) const
{
std::vector<CellI> res;
if (pos.first > 0) {
res.push_back({pos.first - 1, pos.second});
}
if (pos.first + 1 < size_x) {
res.push_back({pos.first + 1, pos.second});
}
if (pos.second > 0) {
res.push_back({pos.first, pos.second - 1});
}
if (pos.second + 1 < size_y) {
res.push_back({pos.first, pos.second + 1});
}
return res;
}
unsigned dist(const CellI &pos1, const CellI &pos2) const {
return std::abs((int) pos1.first - (int) pos2.first) + std::abs((int) pos1.second - (int) pos2.second);
}
};
struct State {
unsigned player_count;
unsigned player_id;
Field field;
std::vector<PlayerInfo> info;
unsigned turn_num;
std::unordered_map<unsigned, std::pair<CellI, unsigned>> capital;
bool exists_not_me = false;
std::mt19937 rnd;
State(unsigned x, unsigned y, unsigned _count, unsigned _id)
: player_count(_count), player_id(_id), field(x, y),
info(_count + 1), turn_num(0)
{
rnd.seed(57444179);
if (player_id > player_count) {
throw std::runtime_error("Invalid player id");
}
}
void update_info()
{
for (unsigned x = 0; x < field.size_x; ++x) {
for (unsigned y = 0; y < field.size_y; ++y) {
const auto &armed = get_armed(field.at({x, y}));
if (!armed) {
continue;
}
if (armed->type == ArmyType::Capital) {
capital[armed->army.owner] = {
{x, y}, armed->army.size};
}
if (armed && armed->army.owner != player_id && armed->army.owner != 0) {
exists_not_me = true;
}
}
}
}
void read_next(std::istream &in)
{
++turn_num;
for (unsigned i = 1; i <= player_count; ++i) {
in >> info[i];
}
field.read_table(in);
update_info();
}
std::optional<int> capture_cost(const CellI &pos) const {
const auto &c = field.at(pos);
if (std::holds_alternative<Visible>(c)) {
const auto &V = std::get<Visible>(c);
if (std::holds_alternative<Armed>(V)) {
const auto &A = std::get<Armed>(V);
if (A.army.owner == player_id) {
return 1 - A.army.size;
} else {
return A.army.size + 1;
}
} else {
return std::nullopt;
}
} else {
const auto &H = std::get<Hidden>(c);
if (H == Hidden::Obstacle) {
return std::nullopt;
} else if (H == Hidden::Empty) {
return 1;
}
}
return std::nullopt;
}
struct PathGeneratorState {
std::mt19937 rnd;
std::deque<CellI> cur;
std::unordered_set<CellI> used;
unsigned units;
unsigned iterations = 0;
unsigned depth;
};
template <class ExitCondition, class ComparePaths>
std::deque<CellI> gen_path(PathGeneratorState &state, ExitCondition &exit, ComparePaths &comp) const
{
++state.iterations;
std::deque<CellI> res = state.cur;
if (state.units == 0 || exit(state)) {
return res;
}
state.used.insert(state.cur.back());
auto neighbors = field.neighbors(state.cur.back());
std::shuffle(neighbors.begin(), neighbors.end(), state.rnd);
for (const auto &cand : neighbors) {
auto c = capture_cost(cand);
if (c && state.units > (unsigned) std::max(0, *c) && state.used.find(cand) == state.used.end()) {
state.cur.push_back(cand);
state.units -= *c;
++state.depth;
auto next_res = gen_path(state, exit, comp);
if (comp(next_res, res)) {
res = std::move(next_res);
}
--state.depth;
state.units += *c;
state.cur.pop_back();
}
}
state.used.erase(state.cur.back());
return res;
}
unsigned my_units(const CellI &pos) const {
const auto &c = field.at(pos);
if (!std::holds_alternative<Visible>(c)) {
return 0;
}
const auto &v = std::get<Visible>(c);
if (!std::holds_alternative<Armed>(v)) {
return 0;
}
const auto &a = std::get<Armed>(v);
if (a.army.owner == player_id) {
return a.army.size;
} else {
return 0;
}
}
std::vector<CellI> my_cells() const {
std::vector<CellI> res;
for (unsigned x = 0; x < field.size_x; ++x) {
for (unsigned y = 0; y < field.size_y; ++y) {
unsigned units = my_units({x, y});
if (units > 0) {
res.push_back({x, y});
}
}
}
return res;
}
struct PathCaptureMetric {
unsigned size;
unsigned dist_sum;
bool operator<(const PathCaptureMetric &other) const {
return size > other.size || (size == other.size && dist_sum < other.dist_sum);
}
};
PathCaptureMetric path_capture_metric(const std::deque<CellI> &x) const {
auto dist = [&](const CellI &pos) -> unsigned {
return field.dist(pos, capital.find(player_id)->second.first);
};
auto filter = [&](const CellI &pos) -> bool {
const auto &c = field.at(pos);
if (std::holds_alternative<Hidden>(c)) {
return true;
}
if (std::holds_alternative<Visible>(c)) {
const auto &v = std::get<Visible>(c);
return (std::holds_alternative<Armed>(v) &&
std::get<Armed>(v).army.owner !=
player_id);
}
return false;
};
unsigned size_x = 0;
unsigned sum_dist = 0;
for (const auto &tmp : x) {
size_x += filter(tmp);
sum_dist += dist(tmp);
}
return {size_x, sum_dist};
}
std::deque<CellI> greedy_path;
Turn greedy_start()
{
if (greedy_path.size() < 2 && (turn_num < 400 && capital[player_id].second <= 10)) {
return Skip{};
} else {
auto cmp_res = [&](const std::deque<CellI> &x, const std::deque<CellI> &y) -> bool {
return path_capture_metric(x) < path_capture_metric(y);
};
auto exit_condition = [](const PathGeneratorState &s) -> bool {
return s.depth >= 10 || s.iterations > 1000;
};
CellI begin;
std::deque<CellI> prev_path;
if (greedy_path.empty() || my_units(greedy_path.front()) <= 1) {
if (turn_num >= 400) {
std::vector<CellI> cells = my_cells();
begin = cells[rnd() % (unsigned) cells.size()];
} else {
begin = capital[player_id].first;
}
} else {
begin = greedy_path.front();
{
PathGeneratorState s;
s.cur = std::move(greedy_path);
for (const auto &CellI : s.cur) {
s.used.insert(CellI);
}
s.iterations = 0;
s.depth = 0;
s.units = my_units(s.cur.back()) - 1;
s.rnd.seed(rnd());
prev_path = gen_path(s, exit_condition, cmp_res);
}
}
{
PathGeneratorState s;
s.cur = {begin};
s.iterations = 0;
s.depth = 0;
s.units = my_units(s.cur.back()) - 1;
s.rnd.seed(rnd());
greedy_path = gen_path(s, exit_condition, cmp_res);
}
if (prev_path.size() >= 2 && path_capture_metric(prev_path) < path_capture_metric(greedy_path)) {
greedy_path = std::move(prev_path);
}
if (greedy_path.size() < 2) {
throw std::logic_error("Failed to build path");
}
CellI cur = greedy_path.front();
greedy_path.pop_front();
auto c = capture_cost(greedy_path.front());
if (c && (unsigned) std::max(*c, 0) > my_units(cur)) {
throw std::logic_error("Tried to capture city");
}
return Move{MoveType::All, cur, greedy_path.front()};
}
}
struct PathCollectMetric {
int collected;
int dist;
bool operator<(const PathCollectMetric &other) {
return collected > other.collected || (collected == other.collected && dist < other.dist);
}
};
PathCollectMetric path_collect_metric(const std::deque<CellI> &path) const {
int cur_collected = 0;
int cur_dist = 0;
for (const auto &pos : path) {
cur_collected -= *capture_cost(pos);
cur_dist = field.dist(pos, capital.find(player_id)->second.first);
}
return {cur_collected, cur_dist};
};
std::deque<CellI> collect_path;
unsigned collected_len;
std::deque<CellI> attack_path;
Turn trahat() {
CellI src = collect_path.front();
if (my_units(src) == 0) {
collected_len = 0;
collect_path.clear();
}
// std::cerr << "Attacking from " << src.first << " " << src.second << std::endl;
std::queue<CellI> q;
std::unordered_map<CellI, CellI> prev;
std::unordered_map<CellI, int> dist;
dist[src] = 0;
prev[src] = src;
q.push(src);
while (!q.empty()) {
auto v = q.front();
q.pop();
for (const auto &u : field.neighbors(v)) {
if (can_pass(field.at(u)) && (dist.find(u) == dist.end() || dist[u] > dist[v] + 1)) {
dist[u] = dist[v] + 1;
q.push(u);
prev[u] = v;
}
}
}
std::optional<CellI> nearest;
for (unsigned x = 0; x < field.size_x; ++x) {
for (unsigned y = 0; y < field.size_y; ++y) {
const auto &armed = get_armed(field.at({x, y}));
if (((!exists_not_me && (!armed || armed->army.owner != player_id)) || (armed && armed->army.owner != player_id && armed->army.owner != 0)) && dist.find({x, y}) != dist.end()) {
if (!nearest || dist[{x, y}] < dist[*nearest]) {
nearest = {x, y};
}
}
}
}
for (const auto &[id, cap] : capital) {
if (id != player_id && dist.find(cap.first) != dist.end()) {
nearest = cap.first;
}
}
if (!nearest) {
collected_len = 0;
collect_path.clear();
return Skip{};
} else {
assert(dist.find(*nearest) != dist.end());
CellI cur_pos = *nearest;
while (prev[cur_pos] != src) {
cur_pos = prev[cur_pos];
}
collect_path = {cur_pos};
return Move{MoveType::All, src, cur_pos};
}
}
Turn midgame() {
unsigned collect_len = std::min((unsigned)my_cells().size() / 2, field.size_x * field.size_y / 40);
if (collected_len + 1 == collect_len) {
return trahat();
} else {
auto cmp_res = [&](const std::deque<CellI> &x, const std::deque<CellI> &y) -> bool {
return path_collect_metric(x) < path_collect_metric(y);
};
auto exit_condition = [&](const PathGeneratorState &s) -> bool {
return s.depth >= 10 || s.cur.size() >= collect_len - collected_len;
};
CellI begin;
std::deque<CellI> prev_path;
if (collect_path.empty() || my_units(collect_path.front()) == 0) {
collected_len = 0;
std::vector<CellI> cells = my_cells();
sort(cells.begin(), cells.end(), [&](const CellI &a, const CellI &b) {
return my_units(a) > my_units(b);
});
begin = cells[rnd() % std::min(30u, (unsigned)cells.size())];
} else {
begin = collect_path.front();
{
PathGeneratorState s;
s.cur = std::move(collect_path);
for (const auto &CellI : s.cur) {
s.used.insert(CellI);
}
s.iterations = 0;
s.depth = 0;
s.units = std::max(0, path_collect_metric(s.cur).collected);
s.rnd.seed(rnd());
prev_path = gen_path(s, exit_condition, cmp_res);
}
}
{
PathGeneratorState s;
s.cur = {begin};
s.used = {};
s.iterations = 0;
s.depth = 0;
s.units = my_units(s.cur.back()) - 1;
s.rnd.seed(rnd());
collect_path = gen_path(s, exit_condition, cmp_res);
}
if (prev_path.size() >= 2 && path_collect_metric(prev_path) < path_collect_metric(collect_path)) {
collect_path = std::move(prev_path);
}
if (collect_path.size() < 2) {
collect_path.clear();
collected_len = 0;
throw std::logic_error("Failed to build path");
}
CellI cur = collect_path.front();
++collected_len;
collect_path.pop_front();
auto c = capture_cost(collect_path.front());
if (c && (unsigned) std::max(*c, 0) > my_units(cur)) {
throw std::logic_error("Tried to capture uncapturable");
}
return Move{MoveType::All, cur, collect_path.front()};
}
return Skip{};
}
void check(const Turn &a) const
{
if (std::holds_alternative<Skip>(a)) {
return;
} else if (std::holds_alternative<Move>(a)) {
const auto &[type, from, to] = std::get<Move>(a);
if (my_units(from) == 0) {
throw std::logic_error("Invalid turn: no units in source");
}
if (field.dist(from, to) != 1) {
throw std::logic_error("Invalid turn: sources does not neighbor destination");
}
const auto &c = field.at(to);
if (!std::holds_alternative<Visible>(c) || !std::holds_alternative<Armed>(std::get<Visible>(c))) {
throw std::logic_error("Invalid turn: unarmed destination");
}
} else {
throw std::logic_error("Invalid turn: empty turn");
}
}
Turn do_turn()
{
if (field.size_x * field.size_y <= 50 && (turn_num <= 2 * field.size_x * field.size_y && !exists_not_me)) {
return greedy_start();
} else {
return midgame();
}
}
};
struct Interactor {
std::istream ∈
std::ostream &out;
void run()
{
unsigned n, m, k, id;
std::cin >> n >> m >> k >> id;
State state(m, n, k, id);
while (true) {
int is_ok;
in >> is_ok;
if (!is_ok) {
break;
}
state.read_next(in);
try {
const auto turn = state.do_turn();
state.check(turn);
out << turn;
} catch (std::exception &e) {
std::cerr << e.what() << "\n";
out << Turn{Skip{}};
}
}
}
};
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cerr.tie(nullptr);
Interactor inter{std::cin, std::cout};
inter.run();
}