-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbc2020.cpp
131 lines (118 loc) · 3.21 KB
/
gbc2020.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
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <thread>
#include <chrono>
using namespace std;
pair<int, int> get_game() {
cout << "game" << endl;
int game_id, remaining_time;
cin >> game_id >> remaining_time;
return {game_id, remaining_time};
}
vector<vector<int>> get_stage(int game_id) {
cout << "stage " << game_id << endl;
int n;
cin >> n;
if (n != 20) throw 1;
vector<vector<int>> a(20, vector<int>(20));
for (auto& b : a) for (auto& x : b) cin >> x;
return a;
}
pair<bool, pair<vector<vector<int>>, vector<vector<int>>>> get_areas(int game_id) {
cout << "areas " << game_id << endl;
string status;
cin >> status;
if (status == "ok") {
vector<vector<int>> a1(20, vector<int>(20));
vector<vector<int>> a2(20, vector<int>(20));
for (auto& b : a1) for (auto& x : b) cin >> x;
for (auto& b : a2) for (auto& x : b) cin >> x;
return {true, {a1, a2}};
} else if (status == "too_many_request") {
return {false, {{}, {}}};
} else {
throw 1;
}
}
bool claim(int game_id, int r, int c, int m) {
cout << "claim " << game_id << " " << r << "-" << c << "-" << m << endl;
string status;
cin >> status;
if (status == "ok") {
return true;
} else if (status == "game_finished") {
return false;
} else {
throw 1;
}
}
struct CalcScore {
const vector<vector<int>>& stage;
const vector<vector<int>>& num_claim;
const vector<vector<int>>& my_claim;
vector<vector<bool>> visited;
CalcScore(const vector<vector<int>>& stage, const vector<vector<int>>& num_claim, const vector<vector<int>>& my_claim)
: stage(stage), num_claim(num_claim), my_claim(my_claim), visited(20, vector<bool>(20)) {}
pair<double, int> f(int r, int c) {
if (r < 0 || r >= 20 || c < 0 || c >= 20 || my_claim[r][c] == 0 || visited[r][c]) {
return {1e+300, 0};
}
visited[r][c] = true;
double r1 = (double)stage[r][c] / num_claim[r][c];
int r2 = 1;
for (auto p : {f(r+1, c), f(r-1, c), f(r, c+1), f(r, c-1)}) {
r1 = min(r1, p.first);
r2 += p.second;
}
return {r1, r2};
}
double calc() {
double score = 0;
for (int i = 0; i < 20; ++ i) {
for (int j = 0; j < 20; ++ j) {
auto p = f(i, j);
score += p.first * p.second;
}
}
return score;
}
};
int main() {
random_device rd;
mt19937 mt(rd());
for (;;) {
auto game = get_game();
auto game_id = game.first;
auto stage = get_stage(game_id);
for (;;) {
auto areas = get_areas(game_id);
if (!areas.first) {
cerr << "too_many_request" << endl;
this_thread::sleep_for(chrono::milliseconds(500));
continue;
}
const auto& num_claim = areas.second.first;
const auto& my_claim = areas.second.second;
auto score = CalcScore(stage, num_claim, my_claim).calc();
cerr << "game_id: " << game_id << " score: " << score << endl;
vector<pair<int,int>> candidate;
for (int i = 0; i < 20; ++ i) {
for (int j = 0; j < 20; ++ j) {
if (my_claim[i][j] == 0) {
candidate.push_back({i, j});
}
}
}
if (candidate.empty()) throw 1;
auto rc = candidate[uniform_int_distribution<int>(0, candidate.size()-1)(mt)];
if (!claim(game_id, rc.first, rc.second, 1)) {
break;
}
}
while (get_game().first == game_id) {
this_thread::sleep_for(chrono::milliseconds(500));
}
}
}