#define pb push_back
#define mt make_tuple
#define mp make_pair
#define is insert
#define ll long long
#define vl vector<ll>
#define sl set<ll>
#define msl multiset<ll>
#define pl pair<ll, ll>
#define vpl vector<pair<ll, ll>>
#define f0r(i, begin, end) for (ll i = begin; i < end; i ++)
#define len(x) x.size()
#include "bits/stdc++.h"
using namespace std;
const int INF = 1e9;
int grid[800][800], N, M;
pair<int, int> arrives[800][800];
pl start, home;
bool process1 (int x, int y, int weight) {
if((x < 0) || (x >= N) || (y < 0) || (y >= N)) {
return false;
}
if (grid[x][y] != INF) {
return false;
}
grid[x][y] = weight;
return true;
}
bool process2 (int x, int y, pair<int, int> weight) {
if((x < 0) || (x >= N) || (y < 0) || (y >= N)) {
return false;
}
if (arrives[x][y].first != INF) {
return false;
}
if (grid[x][y] <= weight.first) {
return false;
}
arrives[x][y] = weight;
return true;
}
bool test (int time) {
f0r (i, 0, N) {
f0r (j, 0, N) {
arrives[i][j] = {INF, INF};
}
}
arrives[start.first][start.second] = {time, 0};
queue<pair<int, int>> q;
q.push(start);
while (!q.empty()) {
int x, y; tie(x, y) = q.front();
q.pop();
auto llegar = arrives[x][y];
if((++llegar.second) == M) {
llegar.first ++;
llegar.second = 0;
}
if (process2(x + 1, y, llegar)) {
q.push({x + 1, y});
}
if (process2(x - 1, y, llegar)) {
q.push({x - 1, y});
}
if (process2(x, y + 1, llegar)) {
q.push({x, y + 1});
}
if (process2(x, y - 1, llegar)) {
q.push({x, y - 1});
}
}
//f0r (i, 0, N) {
// f0r (j, 0, N) {
// cout << i << " " << j << " : " << arrives[i][j].first << ", " << arrives[i][j].second << endl;
// }
//}
return arrives[home.first][home.second].first < INF;
}
ll last_true(ll lo, ll hi) {
lo--;
while (lo < hi) {
ll mid = lo + (hi - lo + 1) / 2;
if (test(mid)) lo = mid;
else hi = mid - 1;
}
return lo;
}
int main () {
cin >> N >> M;
queue<pl> ffill;
f0r (i, 0, N) {
f0r (j, 0, N) {
char a; cin >> a;
if(a == 'T') grid[i][j] = -1;
if(a == 'G') grid[i][j] = INF;
if(a == 'H') {
grid[i][j] = 0;
ffill.push({i, j});
}
if(a == 'M') {
grid[i][j] = INF;
start = {i, j};
}
if(a == 'D') {
grid[i][j] = 1e8;
home = {i, j};
}
}
}
while (!ffill.empty()) {
pl coords = ffill.front();
ll x, y; tie(x, y) = coords;
ffill.pop();
if (process1(x + 1, y, grid[x][y] + 1)) {
ffill.push({x + 1, y});
}
if (process1(x - 1, y, grid[x][y] + 1)) {
ffill.push({x - 1, y});
}
if (process1(x, y + 1, grid[x][y] + 1)) {
ffill.push({x, y + 1});
}
if (process1(x, y - 1, grid[x][y] + 1)) {
ffill.push({x, y - 1});
}
}
cout << last_true(0, grid[start.first][start.second] - 1) << endl;
test(3);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
85 ms |
8652 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
436 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
692 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
820 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
9 ms |
3668 KB |
Output is correct |
34 |
Correct |
10 ms |
3716 KB |
Output is correct |
35 |
Correct |
20 ms |
3724 KB |
Output is correct |
36 |
Correct |
13 ms |
4144 KB |
Output is correct |
37 |
Correct |
16 ms |
4160 KB |
Output is correct |
38 |
Correct |
28 ms |
4156 KB |
Output is correct |
39 |
Correct |
15 ms |
4648 KB |
Output is correct |
40 |
Correct |
16 ms |
4660 KB |
Output is correct |
41 |
Correct |
35 ms |
4692 KB |
Output is correct |
42 |
Correct |
22 ms |
5204 KB |
Output is correct |
43 |
Correct |
19 ms |
5228 KB |
Output is correct |
44 |
Correct |
52 ms |
5204 KB |
Output is correct |
45 |
Correct |
25 ms |
5628 KB |
Output is correct |
46 |
Correct |
23 ms |
5696 KB |
Output is correct |
47 |
Correct |
56 ms |
5760 KB |
Output is correct |
48 |
Correct |
27 ms |
6248 KB |
Output is correct |
49 |
Correct |
26 ms |
6292 KB |
Output is correct |
50 |
Correct |
79 ms |
6296 KB |
Output is correct |
51 |
Correct |
31 ms |
6740 KB |
Output is correct |
52 |
Correct |
31 ms |
6740 KB |
Output is correct |
53 |
Correct |
74 ms |
6828 KB |
Output is correct |
54 |
Correct |
33 ms |
7244 KB |
Output is correct |
55 |
Correct |
38 ms |
7312 KB |
Output is correct |
56 |
Correct |
79 ms |
7240 KB |
Output is correct |
57 |
Correct |
42 ms |
7880 KB |
Output is correct |
58 |
Correct |
41 ms |
7888 KB |
Output is correct |
59 |
Correct |
109 ms |
7880 KB |
Output is correct |
60 |
Correct |
44 ms |
8396 KB |
Output is correct |
61 |
Correct |
47 ms |
8396 KB |
Output is correct |
62 |
Correct |
114 ms |
8396 KB |
Output is correct |
63 |
Correct |
127 ms |
8416 KB |
Output is correct |
64 |
Correct |
189 ms |
8352 KB |
Output is correct |
65 |
Correct |
182 ms |
8424 KB |
Output is correct |
66 |
Correct |
146 ms |
8364 KB |
Output is correct |
67 |
Correct |
139 ms |
8428 KB |
Output is correct |
68 |
Correct |
57 ms |
8376 KB |
Output is correct |
69 |
Correct |
54 ms |
8396 KB |
Output is correct |
70 |
Correct |
58 ms |
8412 KB |
Output is correct |
71 |
Correct |
55 ms |
8472 KB |
Output is correct |
72 |
Correct |
58 ms |
8440 KB |
Output is correct |
73 |
Correct |
43 ms |
8908 KB |
Output is correct |
74 |
Correct |
75 ms |
8948 KB |
Output is correct |
75 |
Correct |
80 ms |
8980 KB |
Output is correct |
76 |
Correct |
66 ms |
8884 KB |
Output is correct |
77 |
Correct |
83 ms |
8908 KB |
Output is correct |
78 |
Correct |
71 ms |
8904 KB |
Output is correct |
79 |
Correct |
67 ms |
8908 KB |
Output is correct |
80 |
Correct |
79 ms |
8920 KB |
Output is correct |
81 |
Correct |
83 ms |
8908 KB |
Output is correct |
82 |
Correct |
69 ms |
8828 KB |
Output is correct |
83 |
Correct |
76 ms |
8840 KB |
Output is correct |
84 |
Correct |
74 ms |
8832 KB |
Output is correct |
85 |
Correct |
91 ms |
8744 KB |
Output is correct |
86 |
Correct |
98 ms |
8776 KB |
Output is correct |
87 |
Correct |
73 ms |
8840 KB |
Output is correct |
88 |
Correct |
77 ms |
8696 KB |
Output is correct |
89 |
Correct |
82 ms |
8668 KB |
Output is correct |
90 |
Correct |
104 ms |
8748 KB |
Output is correct |
91 |
Correct |
115 ms |
8728 KB |
Output is correct |
92 |
Correct |
82 ms |
8744 KB |
Output is correct |