#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using pii = pair<int, int>;
using vi = vector <int>;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define ook order_of_key
#define fbo find_by_order
#define sq(x) (x) * (x)
#define N 805
int x, y, posx, posy, n, s;
int ax[4] = {0, 0, 1, -1};
int ay[4] = {1, -1, 0, 0};
string st[N];
queue <pii> que, q, bfs, mecho;
bool used[N][N], vis[N][N], pass[N][N];
bool check(int curx, int cury){
if (curx < n && cury < n && curx >= 0 && cury >= 0 && !used[curx][cury]) return true;
return false;
}
bool can(int time){;
que = q;
mecho.push(mp(x, y));
memset(pass, 0, sizeof pass);
pass[x][y] = 1;
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++)
used[i][j] = vis[i][j];
while (time--){
bfs = que;
while(!que.empty()) que.pop();
while (!bfs.empty()){
pii pos = bfs.front();
bfs.pop();
for (int i = 0;i < 4;i++)
if (check(pos.F + ax[i], pos.S + ay[i]) && st[pos.F + ax[i]][pos.S + ay[i]] != 'T'
&& st[pos.F + ax[i]][pos.S + ay[i]] != 'D')
que.push(mp(pos.F + ax[i], pos.S + ay[i])), used[pos.F + ax[i]][pos.S + ay[i]] = 1;
}
}
while (!used[posx][posy] && !mecho.empty()){
int tmp = s;
while (tmp--){
bfs = mecho;
while (!mecho.empty()) mecho.pop();
while (!bfs.empty()){
pii pos = bfs.front();
bfs.pop();
if (pos.F == posx && pos.S == posy) return true;
if (!used[pos.F][pos.S])
for (int i = 0;i < 4;i++)
if (check(pos.F + ax[i], pos.S + ay[i]) && st[pos.F + ax[i]][pos.S + ay[i]] != 'T'
&& !pass[pos.F + ax[i]][pos.S + ay[i]])
mecho.push(mp(pos.F + ax[i], pos.S + ay[i])), pass[pos.F + ax[i]][pos.S + ay[i]] = 1;
}
}
bfs = que;
while (!que.empty()) que.pop();
while (!bfs.empty()){
pii pos = bfs.front();
bfs.pop();
for (int i = 0;i < 4;i++)
if (check(pos.F + ax[i], pos.S + ay[i]) && st[pos.F + ax[i]][pos.S + ay[i]] != 'T'
&& st[pos.F + ax[i]][pos.S + ay[i]] != 'D')
que.push(mp(pos.F + ax[i], pos.S + ay[i])), used[pos.F + ax[i]][pos.S + ay[i]] = 1;
}
}
return false;
}
int main (){
cin >> n>> s;
for (int i = 0;i < n;i++)
cin >> st[i];
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++){
if (st[i][j] == 'M') x = i, y = j;
if (st[i][j] == 'D') posx = i, posy = j;
if (st[i][j] == 'H') q.push(mp(i, j)), vis[i][j] = 1;
}
int l = 0, r = n * n;
while (l != r){
int mid = (l + r + 1) / 2;
if (can(mid)) l = mid;
else r = mid - 1;
}
// for (int i = 0;;i++)
// if (!can(i)){
// cout << i - 1;
// return 0;
// }
if (can(l)) cout << l<< endl;
else cout << -1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
2 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
411 ms |
3064 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1068 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
5 ms |
1024 KB |
Output is correct |
13 |
Correct |
6 ms |
1024 KB |
Output is correct |
14 |
Correct |
8 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1024 KB |
Output is correct |
16 |
Correct |
3 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
1024 KB |
Output is correct |
18 |
Correct |
4 ms |
940 KB |
Output is correct |
19 |
Correct |
4 ms |
1024 KB |
Output is correct |
20 |
Correct |
4 ms |
1024 KB |
Output is correct |
21 |
Correct |
4 ms |
1024 KB |
Output is correct |
22 |
Correct |
4 ms |
1024 KB |
Output is correct |
23 |
Correct |
5 ms |
1024 KB |
Output is correct |
24 |
Correct |
5 ms |
1152 KB |
Output is correct |
25 |
Correct |
6 ms |
1024 KB |
Output is correct |
26 |
Correct |
4 ms |
1024 KB |
Output is correct |
27 |
Correct |
6 ms |
1024 KB |
Output is correct |
28 |
Correct |
5 ms |
1024 KB |
Output is correct |
29 |
Correct |
7 ms |
1024 KB |
Output is correct |
30 |
Correct |
5 ms |
1024 KB |
Output is correct |
31 |
Correct |
8 ms |
1024 KB |
Output is correct |
32 |
Correct |
7 ms |
1024 KB |
Output is correct |
33 |
Correct |
127 ms |
1480 KB |
Output is correct |
34 |
Correct |
54 ms |
1452 KB |
Output is correct |
35 |
Incorrect |
53 ms |
1544 KB |
Output isn't correct |
36 |
Correct |
195 ms |
1536 KB |
Output is correct |
37 |
Correct |
105 ms |
1536 KB |
Output is correct |
38 |
Incorrect |
71 ms |
1664 KB |
Output isn't correct |
39 |
Correct |
201 ms |
1612 KB |
Output is correct |
40 |
Correct |
93 ms |
1536 KB |
Output is correct |
41 |
Incorrect |
87 ms |
1536 KB |
Output isn't correct |
42 |
Correct |
279 ms |
1920 KB |
Output is correct |
43 |
Correct |
116 ms |
1920 KB |
Output is correct |
44 |
Incorrect |
114 ms |
1912 KB |
Output isn't correct |
45 |
Correct |
345 ms |
2032 KB |
Output is correct |
46 |
Correct |
151 ms |
1920 KB |
Output is correct |
47 |
Incorrect |
152 ms |
1920 KB |
Output isn't correct |
48 |
Correct |
452 ms |
2048 KB |
Output is correct |
49 |
Correct |
196 ms |
2040 KB |
Output is correct |
50 |
Correct |
179 ms |
2044 KB |
Output is correct |
51 |
Correct |
592 ms |
2284 KB |
Output is correct |
52 |
Correct |
281 ms |
2296 KB |
Output is correct |
53 |
Incorrect |
205 ms |
2176 KB |
Output isn't correct |
54 |
Correct |
514 ms |
2380 KB |
Output is correct |
55 |
Correct |
255 ms |
2176 KB |
Output is correct |
56 |
Incorrect |
237 ms |
2396 KB |
Output isn't correct |
57 |
Correct |
775 ms |
2348 KB |
Output is correct |
58 |
Correct |
307 ms |
2304 KB |
Output is correct |
59 |
Incorrect |
311 ms |
2296 KB |
Output isn't correct |
60 |
Correct |
971 ms |
2552 KB |
Output is correct |
61 |
Correct |
301 ms |
2424 KB |
Output is correct |
62 |
Incorrect |
448 ms |
2456 KB |
Output isn't correct |
63 |
Correct |
423 ms |
2552 KB |
Output is correct |
64 |
Correct |
477 ms |
2424 KB |
Output is correct |
65 |
Correct |
445 ms |
2424 KB |
Output is correct |
66 |
Correct |
474 ms |
2552 KB |
Output is correct |
67 |
Correct |
401 ms |
2424 KB |
Output is correct |
68 |
Correct |
457 ms |
3140 KB |
Output is correct |
69 |
Correct |
353 ms |
3168 KB |
Output is correct |
70 |
Correct |
459 ms |
3084 KB |
Output is correct |
71 |
Correct |
487 ms |
3192 KB |
Output is correct |
72 |
Correct |
505 ms |
3192 KB |
Output is correct |
73 |
Correct |
377 ms |
3708 KB |
Output is correct |
74 |
Correct |
482 ms |
3832 KB |
Output is correct |
75 |
Incorrect |
444 ms |
3708 KB |
Output isn't correct |
76 |
Incorrect |
406 ms |
3700 KB |
Output isn't correct |
77 |
Correct |
401 ms |
3648 KB |
Output is correct |
78 |
Correct |
451 ms |
3576 KB |
Output is correct |
79 |
Correct |
408 ms |
3524 KB |
Output is correct |
80 |
Correct |
501 ms |
3296 KB |
Output is correct |
81 |
Correct |
521 ms |
3380 KB |
Output is correct |
82 |
Incorrect |
386 ms |
3276 KB |
Output isn't correct |
83 |
Correct |
451 ms |
3232 KB |
Output is correct |
84 |
Correct |
553 ms |
3464 KB |
Output is correct |
85 |
Correct |
479 ms |
3292 KB |
Output is correct |
86 |
Correct |
489 ms |
3348 KB |
Output is correct |
87 |
Correct |
453 ms |
3252 KB |
Output is correct |
88 |
Correct |
545 ms |
3152 KB |
Output is correct |
89 |
Correct |
478 ms |
3152 KB |
Output is correct |
90 |
Correct |
454 ms |
3196 KB |
Output is correct |
91 |
Correct |
404 ms |
3240 KB |
Output is correct |
92 |
Correct |
497 ms |
3180 KB |
Output is correct |