-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission.cpp
1213 lines (1020 loc) · 35.4 KB
/
submission.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cassert>
#include <cstring>
#include <cmath>
#include <tuple>
#include <algorithm>
#include <chrono>
#include <bitset>
#include <functional>
#include <stdint.h>
#ifdef LOCAL_TEST
#include "./dbg.hpp"
#define MAX_RUNTIME 5000
#else
#define dbg(...)
#define MAX_RUNTIME 10000
#endif
#define REP(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
const int MAX_N = 50;
const int CANCEL_LIMIT = MAX_RUNTIME / 14;
/* Written in 2019 by David Blackman and Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
/* This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators.
It has excellent (sub-ns) speed, a state (256 bits) that is large
enough for any parallel application, and it passes all tests we are
aware of.
For generating just floating-point numbers, xoshiro256+ is even faster.
The state must be seeded so that it is not everywhere zero. If you have
a 64-bit seed, we suggest to seed a splitmix64 generator and use its
output to fill s. */
namespace rng {
/* Written in 2015 by Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
/* This is a fixed-increment version of Java 8's SplittableRandom generator
See http://dx.doi.org/10.1145/2714064.2660195 and
http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html
It is a very fast generator passing BigCrush, and it can be useful if
for some reason you absolutely want 64 bits of state. */
namespace splitmix64 {
static uint64_t x; /* The state can be seeded with any value. */
uint64_t next() {
uint64_t z = (x += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
}
static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}
static uint64_t s[4];
void seed(uint64_t seed) {
splitmix64::x = seed;
s[0] = splitmix64::next();
s[1] = splitmix64::next();
s[2] = splitmix64::next();
s[3] = splitmix64::next();
}
uint64_t next(void) {
const uint64_t result = rotl(s[0] + s[3], 23) + s[0];
const uint64_t t = s[1] << 17;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = rotl(s[3], 45);
return result;
}
/* This is the jump function for the generator. It is equivalent
to 2^128 calls to next(); it can be used to generate 2^128
non-overlapping subsequences for parallel computations. */
void jump(void) {
static const uint64_t JUMP[] = { 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
uint64_t s0 = 0;
uint64_t s1 = 0;
uint64_t s2 = 0;
uint64_t s3 = 0;
for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
for(int b = 0; b < 64; b++) {
if (JUMP[i] & UINT64_C(1) << b) {
s0 ^= s[0];
s1 ^= s[1];
s2 ^= s[2];
s3 ^= s[3];
}
next();
}
s[0] = s0;
s[1] = s1;
s[2] = s2;
s[3] = s3;
}
/* This is the long-jump function for the generator. It is equivalent to
2^192 calls to next(); it can be used to generate 2^64 starting points,
from each of which jump() will generate 2^64 non-overlapping
subsequences for parallel distributed computations. */
void long_jump(void) {
static const uint64_t LONG_JUMP[] = { 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
uint64_t s0 = 0;
uint64_t s1 = 0;
uint64_t s2 = 0;
uint64_t s3 = 0;
for(int i = 0; i < sizeof LONG_JUMP / sizeof *LONG_JUMP; i++)
for(int b = 0; b < 64; b++) {
if (LONG_JUMP[i] & UINT64_C(1) << b) {
s0 ^= s[0];
s1 ^= s[1];
s2 ^= s[2];
s3 ^= s[3];
}
next();
}
s[0] = s0;
s[1] = s1;
s[2] = s2;
s[3] = s3;
}
}
using Pos = pair<int, int>;
struct Constraint {
int value;
set<Pos> positions;
Constraint(int value_, set<Pos> positions_): value(value_), positions(positions_) {}
int max_one() const {
return value;
}
int max_zero() const {
return positions.size() - value;
}
};
using Constraints = vector<Constraint>;
// https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp
// CC0 license
struct UnionFind {
public:
UnionFind() : _n(0) {}
UnionFind(int n) : _n(n), parent_or_size(n, -1) {}
int merge(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y)
return x;
if (-parent_or_size[x] < -parent_or_size[y])
std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0)
return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(_n), group_size(_n);
for (int i = 0; i < _n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(_n);
for (int i = 0; i < _n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < _n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(
std::remove_if(result.begin(), result.end(),
[&](const std::vector<int> &v) { return v.empty(); }),
result.end());
return result;
}
private:
int _n;
// root node: -1 * component size
// otherwise: parent
std::vector<int> parent_or_size;
};
struct Params {
int N;
int M;
int D;
};
class Cell {
const int HIDDEN = -1;
const int BOMB = -2;
const int SAFE = -3;
int value;
public:
Cell() {
value = HIDDEN;
}
bool is_hidden() const {
return value == HIDDEN;
}
bool is_bomb() const {
return value == BOMB;
}
bool has_value(int x) {
assert(x >= 0);
return value == x;
}
bool is_value() const {
return value >= 0;
}
bool is_safe() const {
return value == SAFE;
}
int get_value() const {
assert(is_value());
return value;
}
void set_value(int value) {
assert(is_hidden() || is_safe());
assert(value >= 0);
this->value = value;
}
void set_bomb() {
assert(is_hidden());
this->value = BOMB;
}
void set_safe() {
assert(is_hidden());
this->value = SAFE;
}
};
class Command {
enum Type {
Stop,
Open,
Flag,
};
Type type;
int _row;
int _col;
Command(Type type) {
this->type = type;
}
Command(Type type, int row, int col) {
this->type = type;
this->_row = row;
this->_col = col;
}
public:
static Command stop() {
return Command(Type::Stop);
}
static Command open(int r, int c) {
return Command(Type::Open, r, c);
}
static Command flag(int r, int c) {
return Command(Type::Flag, r, c);
}
int row() const {
return _row;
}
int col() const {
return _col;
}
bool is_stop() {
return type == Type::Stop;
}
bool is_open() {
return type == Type::Open;
}
bool is_flag() {
return type == Type::Flag;
}
string to_string() {
if (is_stop()) {
return "STOP";
}
if (is_open()) {
return "G " + ::to_string(_row) + " " + ::to_string(_col);
}
if (is_flag()) {
return "F " + ::to_string(_row) + " " + ::to_string(_col);
}
assert(false);
}
};
struct Stats {
int guess_count = 0;
int random_guess_count = 0;
int cancel_count = 0;
double max_score = 0.0;
void print() {
cerr << "guess_count: " << guess_count << endl;
cerr << "random_guess_count: " << random_guess_count << endl;
cerr << "max_score: " << max_score << endl;
cerr << "cancel_count: " << cancel_count << endl;
}
};
class Solver {
Params params;
Stats stats;
int grid_unknown_count = 0;
int grid_bomb_count = 0;
int score_uncover = 0;
int score_mine_hit = 0;
bool backtrace_timeout = false;
vector<vector<Cell>> judge_grid;
vector<vector<Cell>> grid;
long runtime = 0;
std::chrono::system_clock::time_point last_update = chrono::system_clock::now();
std::chrono::system_clock::time_point now = chrono::system_clock::now();
int now_counter = 0;
void set_runtime(long runtime) {
this->runtime = runtime;
last_update = chrono::system_clock::now();
now = last_update;
now_counter = 0;
}
long get_runtime(bool force=false) {
if (force || ++now_counter == 1000) {
now_counter = 0;
now = chrono::system_clock::now();
}
return runtime + chrono::duration_cast<std::chrono::milliseconds>(now-last_update).count();
}
vector<Command> next_commands;
int count_safe_neighbors[MAX_N][MAX_N];
int count_bomb_neighbors[MAX_N][MAX_N];
set<Pos> unknown_neighbors[MAX_N][MAX_N];
vector<Pos> neighbors[MAX_N][MAX_N];
set<Pos> positions_with_unknown_values;
bool with_in(int r1, int c1, int r2, int c2, int D) {
return (r1 - r2) * (r1 - r2) + (c1 - c2) * (c1 - c2) <= D;
}
double calc_uncover_ratio() {
const int all = params.N * params.N - params.M;
return (double)score_uncover/(double)all;
}
double score() {
return calc_uncover_ratio() / (1.0 + score_mine_hit);
}
public:
Solver(int N, int M, int D, int row, int col) {
params.N = N;
params.M = M;
params.D = D;
grid = vector<vector<Cell>>(N, vector<Cell>(N, Cell()));
judge_grid = vector<vector<Cell>>(N, vector<Cell>(N, Cell()));
memset(count_safe_neighbors, 0, sizeof(count_safe_neighbors));
memset(count_bomb_neighbors, 0, sizeof(count_bomb_neighbors));
REP(r, N) REP(c, N) {
REP(nr, N) REP(nc, N) {
if (with_in(r, c, nr, nc, D)) {
neighbors[r][c].push_back(make_pair(nr, nc));
unknown_neighbors[r][c].insert(make_pair(nr, nc));
}
}
}
grid_unknown_count = params.N * params.N;
judge_update_value(row, col, 0, 0);
}
bool is_game_end() {
const int all = params.N * params.N - params.M;
return all == score_uncover;
}
bool is_last_move() {
const int all = params.N * params.N - params.M;
return all == score_uncover + 1;
}
Command choose_random_unknown() {
while(true) {
int r = rng::next() % params.N;
int c = rng::next() % params.N;
if (grid[r][c].is_hidden() && judge_grid[r][c].is_hidden()) {
return Command::open(r, c);
}
}
}
Constraints extract_constraints() {
Constraints results;
for(auto p : positions_with_unknown_values) {
int r, c;
tie(r, c) = p;
assert(grid[r][c].is_value() && !unknown_neighbors[r][c].empty());
const int value = grid[r][c].get_value() - count_bomb_neighbors[r][c];
assert(unknown_neighbors[r][c].size() > value);
results.push_back(Constraint(value, set<Pos>(unknown_neighbors[r][c].begin(), unknown_neighbors[r][c].end())));
}
return results;
}
vector<pair<int, Pos>> find_new_neighbors() {
set<Pos> all_unknown_neighbors;
for(auto p : positions_with_unknown_values) {
int r, c;
tie(r, c) = p;
assert(grid[r][c].is_value() && !unknown_neighbors[r][c].empty());
for(auto np : unknown_neighbors[r][c]) {
all_unknown_neighbors.insert(np);
}
}
map<Pos, int> counter;
for(auto p : all_unknown_neighbors) {
int r, c;
tie(r, c) = p;
for(auto np : unknown_neighbors[r][c]) {
if (!all_unknown_neighbors.count(np)) {
counter[np] += 1;
}
}
}
vector<pair<int, Pos>> result;
for(auto p : counter) {
result.push_back(make_pair(p.second, p.first));
}
sort(result.begin(), result.end());
reverse(result.begin(), result.end());
return result;
}
vector<Constraints> split_constraints(const Constraints& constraints) {
UnionFind uf(constraints.size());
vector<vector<int>> c_id(params.N, vector<int>(params.N, -1));
for(int i = 0; i < constraints.size(); i++) {
for (const auto& p : constraints[i].positions) {
const int j = c_id[p.first][p.second];
if (j == -1) {
c_id[p.first][p.second] = i;
} else {
uf.merge(i, j);
}
}
}
const auto groups = uf.groups();
vector<Constraints> result;
for(const auto& group : groups) {
Constraints list;
for(int i : group) {
list.push_back(constraints[i]);
}
result.push_back(list);
}
return result;
}
vector<int> _dfs_count_zero;
vector<int> _dfs_count_one;
vector<double> _dfs_result_ones;
int _dfs_result_total;
Constraints _dfs_constraints;
bool _dfs_cancel;
vector<vector<int>> _dfs_p2c;
vector<int> _dfs_p_count;
// no init
int _dfs_mines_len;
int _dfs_mines[MAX_N * MAX_N];
double _dfs_mines_add[MAX_N * MAX_N];
void dfs(int index){
if (index == _dfs_p2c.size()) {
_dfs_result_total += 1;
for (int i = 0; i < _dfs_mines_len; i++) {
_dfs_result_ones[_dfs_mines[i]] += _dfs_mines_add[i];
}
return;
}
if (backtrace_timeout) {
return;
}
if (_dfs_cancel) {
return;
}
if (get_runtime() > MAX_RUNTIME * 0.95) {
cerr << "dfs timeout!" << endl;
dbg(get_runtime());
dbg(_dfs_p2c.size());
dbg(_dfs_constraints.size());
dbg(_dfs_result_total);
backtrace_timeout = true;
return;
}
if (params.D >= 8 && get_runtime() - runtime > CANCEL_LIMIT) {
_dfs_cancel = true;
return;
}
if (_dfs_p_count[index] == 1) {
{
int violate = _dfs_p2c[index].size();
for (int i = 0; i < _dfs_p2c[index].size(); i++) {
const int x = _dfs_p2c[index][i];
if (++_dfs_count_zero[x] > _dfs_constraints[x].max_zero()) {
violate = i;
break;
}
}
if (violate == _dfs_p2c[index].size()) {
dfs(index + 1);
violate--;
}
for (int i = 0; i <= violate; i++) {
const int x = _dfs_p2c[index][i];
--_dfs_count_zero[x];
}
}
{
int violate = _dfs_p2c[index].size();
for (int i = 0; i < _dfs_p2c[index].size(); i++) {
const int x = _dfs_p2c[index][i];
if (++_dfs_count_one[x] > _dfs_constraints[x].max_one()) {
violate = i;
break;
}
}
if (violate == _dfs_p2c[index].size()) {
_dfs_mines[_dfs_mines_len] = index;
_dfs_mines_add[_dfs_mines_len] = 1.0;
_dfs_mines_len++;
dfs(index + 1);
_dfs_mines_len--;
violate--;
}
for (int i = 0; i <= violate; i++) {
const int x = _dfs_p2c[index][i];
--_dfs_count_one[x];
}
}
} else {
assert(_dfs_p_count[index] >= 2);
for (int use = 0; use <= _dfs_p_count[index]; use++) {
int violate = _dfs_p2c[index].size();
for (int i = 0; i < _dfs_p2c[index].size(); i++) {
const int x = _dfs_p2c[index][i];
_dfs_count_zero[x] += _dfs_p_count[index] - use;
_dfs_count_one[x] += use;
if ((_dfs_count_zero[x] > _dfs_constraints[x].max_zero())
|| (_dfs_count_one[x] > _dfs_constraints[x].max_one())) {
violate = i;
break;
}
}
if (violate == _dfs_p2c[index].size()) {
_dfs_mines[_dfs_mines_len] = index;
_dfs_mines_add[_dfs_mines_len] = (double)use/_dfs_p_count[index];
_dfs_mines_len++;
dfs(index + 1);
_dfs_mines_len--;
violate--;
}
for (int i = 0; i <= violate; i++) {
const int x = _dfs_p2c[index][i];
_dfs_count_zero[x] -= _dfs_p_count[index] - use;
_dfs_count_one[x] -= use;
}
}
}
}
void backtrace(const Constraints& constraints, vector<pair<double, Pos>>& probabilities) {
if (backtrace_timeout) {
return;
}
set<Pos> point_set;
for(const auto& c : constraints) {
for(const auto& p : c.positions) {
point_set.insert(p);
}
}
vector<Pos> points(point_set.begin(), point_set.end());
map<Pos, int> occurance;
for(const auto& c : constraints) {
for(const auto& p : c.positions) {
occurance[p]++;
}
}
sort(points.begin(), points.end(), [&](const Pos& a, const Pos& b){
return occurance[a] > occurance[b];
});
assert(occurance[points[0]] >= occurance[points[points.size() - 1]]);
vector<vector<int>> p2c(points.size(), vector<int>());
for(int i = 0; i < constraints.size(); i++) {
for (int j = 0; j < points.size(); j++) {
if (constraints[i].positions.count(points[j])) {
p2c[j].push_back(i);
}
}
}
map<vector<int>, int> counter;
for(int i = 0; i < points.size(); i++) {
counter[p2c[i]] += 1;
}
/*
for(int i = 0; i < points.size(); i++) {
for (int j : p2c[i]) {
cerr << j << " ";
}
cerr << endl;
}
cerr << "---" << endl;
for(auto p : counter) {
if (p.second >= 2) {
cerr << "[";
for(auto x : p.first) {
cerr << x << " ";
}
cerr << "] " << p.second << endl;
}
}
*/
vector<vector<int>> point_groups;
{
map<vector<int>, int> group_id;
for(int i = 0; i < points.size(); i++) {
if (params.D <= 4) { // TODO: 5 or 3?
point_groups.push_back(vector<int>(1, i));
continue;
}
if (!group_id.count(p2c[i])) {
group_id[p2c[i]] = point_groups.size();
point_groups.push_back(vector<int>());
}
point_groups[group_id[p2c[i]]].push_back(i);
}
}
_dfs_p_count = vector<int>(point_groups.size());
_dfs_p2c = vector<vector<int>>(point_groups.size());
for(int i = 0; i < point_groups.size(); i++) {
_dfs_p2c[i] = p2c[point_groups[i][0]];
_dfs_p_count[i] = point_groups[i].size();
}
this->_dfs_constraints = constraints;
_dfs_count_zero = vector<int>(constraints.size(), 0);
_dfs_count_one = vector<int>(constraints.size(), 0);
_dfs_result_ones = vector<double>(point_groups.size());
_dfs_result_total = 0;
_dfs_cancel = false;
_dfs_mines_len = 0;
dfs(0);
if (backtrace_timeout) {
return;
}
if (_dfs_cancel) {
cerr << "cancel!" << endl;
stats.cancel_count ++;
for(int i = 0; i < point_groups.size(); i++) {
if (_dfs_result_total > 0 && _dfs_result_total == _dfs_result_ones[i]) {
continue;
}
dbg(_dfs_result_total);
dbg(_dfs_result_ones[i]);
probabilities.push_back(make_pair(0.0, points[point_groups[i][0]])); // make to pick up first
break;
}
return;
}
assert(_dfs_result_total > 0);
for(int i = 0; i < point_groups.size(); i++) {
const double count_one = _dfs_result_ones[i];
const int count_all = _dfs_result_total;
if (count_one == 0) {
for(int j : point_groups[i]) {
const auto& p = points[j];
int r, c;
tie(r, c) = p;
if (!grid[r][c].is_hidden()) {
continue;
}
update_safe(r, c);
}
} else if (count_one == count_all) {
for(int j : point_groups[i]) {
const auto& p = points[j];
int r, c;
tie(r, c) = p;
if (!grid[r][c].is_hidden()) {
continue;
}
update_bomb(r, c);
}
} else {
assert(count_one > 1e-6 && ((double)count_all - count_one) > 1e-6);
for(int j : point_groups[i]) {
const auto& p = points[j];
int r, c;
tie(r, c) = p;
if (!grid[r][c].is_hidden()) {
continue;
}
probabilities.push_back(make_pair((double)count_one/(double)count_all, p));
}
}
}
}
vector<pair<double, Pos>> global_search() {
if (backtrace_timeout) {
return vector<pair<double, Pos>>();
}
cerr << "global search!" << endl;
const auto constraints = extract_constraints();
const auto constraint_groups = split_constraints(constraints);
vector<pair<double, Pos>> probabilities;
for (const auto& group : constraint_groups) {
backtrace(group, probabilities);
dbg(_dfs_result_total);
if (backtrace_timeout) {
break;
}
}
dbg(next_commands.size());
dbg(get_runtime(true) - runtime);
sort(probabilities.begin(), probabilities.end());
return probabilities;
}
void insert_all_opens() {
REP(r, params.N) REP(c, params.N) {
if (grid[r][c].is_hidden() && judge_grid[r][c].is_hidden()) {
next_commands.push_back(Command::open(r, c));
}
}
}
void print_stats(string reason) {
cerr << "reason: " << reason << endl;
cerr << "runtime: " << get_runtime(true) << endl;
cerr << "uncover_ratio: " << calc_uncover_ratio() << endl;
cerr << "mine_hit: " << score_mine_hit << endl;
stats.print();
cerr.flush();
}
double find_maximum_score(
const int all,
const int rest,
const int M, // mine_hit
const double g
){
const int cur = all - rest;
const auto f = [&](double x){
double s1 = (cur + x) / all;
double s2 = 1.0 / (1.0 + M + g * x);
return s1 * s2;
};
double low = 0;
double high = rest;
REP(_, 100) {
const double c1 = (low * 2 + high) / 3;
const double c2 = (low + high * 2) / 3;
if (f(c1) < f(c2)) {
low = c1;
} else {
high = c2;
}
}
return f(low);
}
double estimate_best_score(double prob) {
map<int, tuple<double, double, double>> param_map;
param_map[1] = make_tuple(0.24931335, 17.86378303, -2.76674817);
//param_map[2] = make_tuple(4.52466102e-03, 2.69382644e+01, -1.37446064e-01);
map<int, double> k_map;
k_map[1] = 0.5;
//k_map[2] = 0.5;
if (param_map.count(params.D)) {
const int all = params.N * params.N - params.M;
const int cur = score_uncover;
const int rest = all - cur;
const double r = (double)params.M/(double)params.N/(double)params.N; // ratio (0.1~0.3)
const double a = get<0>(param_map[params.D]);
const double b = get<1>(param_map[params.D]);
const double c = get<2>(param_map[params.D]);
const double g = (a * exp(b * r) + c) / 1000.0;
const double k = k_map[params.D];
const double maximum_score_good = 1.0 / (1.0 + score_mine_hit + g * rest * k);
const double maximum_score_bad = 1.0 / (1.0 + score_mine_hit + 1 + g * rest * k);
//const double maximum_score_good = find_maximum_score(all, rest, score_mine_hit, g * k);
//const double maximum_score_bad = find_maximum_score(all, rest, score_mine_hit + 1, g * k);
const double result = prob * maximum_score_bad + (1 - prob) * maximum_score_good;
/*
dbg(score());
dbg(maximum_score_good);
dbg(maximum_score_bad);
dbg(result);
*/
return result;
}
if (params.D >= 4 && params.D <= 5) {
const double ESTIMATED_ADD_BOMB = 4.0;
return 1.0 / (1.0 + score_mine_hit + ESTIMATED_ADD_BOMB * prob);
} else if (params.D >= 9) {
const double ESTIMATED_ADD_BOMB = 4.0;
return 1.0 / (1.0 + score_mine_hit + ESTIMATED_ADD_BOMB * prob);
} else {
const double ESTIMATED_ADD_BOMB = 4.0;
return 1.0 / (1.0 + score_mine_hit + ESTIMATED_ADD_BOMB * prob);
}
}
Command decide_next_command() {
assert(!is_game_end());
vector<pair<double, Pos>> probabilities;
if (next_commands.empty()) {
probabilities = global_search();
}
if (grid_bomb_count == params.M && grid_unknown_count > 0 && next_commands.empty()) {
insert_all_opens();
}
if (!next_commands.empty()) {
Command next = next_commands.back();
next_commands.pop_back();
if (next.is_open() && is_last_move()) {
this->score_uncover += 1; // to print correct stats
stats.max_score = max(score(), stats.max_score);
print_stats("win");
this->score_uncover -= 1;
}
return next;
}
if (backtrace_timeout) {
print_stats("timeout");
return Command::stop();
}
const int rest = params.M - grid_bomb_count;
const int all = grid_unknown_count;
const double default_prob = (double)rest/(double)all;
function<Command()> factory = [this]() {
return this->choose_random_unknown();
};
double prob = default_prob;
bool random_guess = true;
if (!probabilities.empty()) {
const auto best = probabilities[0];
cerr << "guess!" << endl;