#include <bits/stdc++.h>
using namespace std;
bool visited[805][805];
int distBee[805][805];
int distBear[805][805];
char g[805][805];
int dr[] = {-1,0,1,0};
int dc[] = {0,1,0,-1};
int n,s,sr,sc,er,ec,mxtime;
bool success;
bool possible(int t) {
memset(visited,0,sizeof visited);
queue < pair <pair<int,int>,int> > bfs;
bfs.push({{sr,sc},0});
visited[sr][sc] = true;
distBear[sr][sc] = t;
while (!bfs.empty()) {
int curR = bfs.front().first.first;
int curC = bfs.front().first.second;
int steps = bfs.front().second;
bfs.pop();
for (int i=0;i<4;i++) {
int nr = curR + dr[i];
int nc = curC + dc[i];
if (nr<1||nr>n||nc<1||nc>n||visited[nr][nc]||g[nr][nc]=='T'||distBear[curR][curC]+((steps+1)%s==0?1:0)>=distBee[nr][nc]) continue;
visited[nr][nc] = true;
distBear[nr][nc] = distBear[curR][curC] + ((steps+1)%s==0?1:0);
bfs.push({{nr,nc},steps+1});
}
}
if (visited[er][ec]) {
success = true;
return true;
}
return false;
}
int main() {
queue < pair<int,int> > bfs;
cin >> n >> s;
for (int i=1;i<=n;i++) {
for (int j=1;j<=n;j++) {
cin >> g[i][j];
distBee[i][j]=1000000;
if (g[i][j]=='M') sr = i,sc = j;
else if (g[i][j]=='D') er = i,ec = j;
else if (g[i][j]=='H') {
visited[i][j] = true;
distBee[i][j] = 0;
bfs.push({i,j});
}
}
}
while (!bfs.empty()) {
int curR = bfs.front().first;
int curC = bfs.front().second;
bfs.pop();
for (int i=0;i<4;i++) {
int nr = curR + dr[i];
int nc = curC + dc[i];
if (nr<1||nr>n||nc<1||nc>n||visited[nr][nc]||g[nr][nc]!='G') continue;
visited[nr][nc] = true;
distBee[nr][nc] = distBee[curR][curC]+1;
mxtime = max(mxtime,distBee[nr][nc]);
bfs.push({nr,nc});
}
}
int lo = 0,hi = mxtime;
while (lo<hi) {
int mid = (lo+hi+1)/2;
if (possible(mid)) lo = mid;
else hi = mid-1;
}
if (!success) cout << -1 << endl;
else cout << lo << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
132 ms |
5672 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
1 ms |
1108 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
10 ms |
2496 KB |
Output is correct |
34 |
Correct |
13 ms |
2980 KB |
Output is correct |
35 |
Correct |
24 ms |
3436 KB |
Output is correct |
36 |
Correct |
15 ms |
2840 KB |
Output is correct |
37 |
Correct |
15 ms |
3104 KB |
Output is correct |
38 |
Correct |
46 ms |
3668 KB |
Output is correct |
39 |
Correct |
16 ms |
3028 KB |
Output is correct |
40 |
Correct |
19 ms |
3372 KB |
Output is correct |
41 |
Correct |
46 ms |
4068 KB |
Output is correct |
42 |
Correct |
19 ms |
3308 KB |
Output is correct |
43 |
Correct |
20 ms |
3604 KB |
Output is correct |
44 |
Correct |
56 ms |
4472 KB |
Output is correct |
45 |
Correct |
26 ms |
3536 KB |
Output is correct |
46 |
Correct |
27 ms |
3896 KB |
Output is correct |
47 |
Correct |
90 ms |
4832 KB |
Output is correct |
48 |
Correct |
29 ms |
3736 KB |
Output is correct |
49 |
Correct |
28 ms |
4136 KB |
Output is correct |
50 |
Correct |
80 ms |
5180 KB |
Output is correct |
51 |
Correct |
31 ms |
3916 KB |
Output is correct |
52 |
Correct |
35 ms |
4456 KB |
Output is correct |
53 |
Correct |
97 ms |
5540 KB |
Output is correct |
54 |
Correct |
39 ms |
4228 KB |
Output is correct |
55 |
Correct |
40 ms |
4704 KB |
Output is correct |
56 |
Correct |
141 ms |
5892 KB |
Output is correct |
57 |
Correct |
41 ms |
4400 KB |
Output is correct |
58 |
Correct |
43 ms |
5044 KB |
Output is correct |
59 |
Correct |
129 ms |
6244 KB |
Output is correct |
60 |
Correct |
53 ms |
4688 KB |
Output is correct |
61 |
Correct |
58 ms |
5276 KB |
Output is correct |
62 |
Correct |
172 ms |
6580 KB |
Output is correct |
63 |
Incorrect |
113 ms |
6508 KB |
Output isn't correct |
64 |
Correct |
177 ms |
6608 KB |
Output is correct |
65 |
Correct |
187 ms |
6604 KB |
Output is correct |
66 |
Correct |
158 ms |
6524 KB |
Output is correct |
67 |
Correct |
131 ms |
6560 KB |
Output is correct |
68 |
Incorrect |
74 ms |
6280 KB |
Output isn't correct |
69 |
Correct |
78 ms |
6624 KB |
Output is correct |
70 |
Correct |
67 ms |
6564 KB |
Output is correct |
71 |
Correct |
69 ms |
6628 KB |
Output is correct |
72 |
Incorrect |
54 ms |
4624 KB |
Output isn't correct |
73 |
Incorrect |
84 ms |
6776 KB |
Output isn't correct |
74 |
Correct |
80 ms |
6880 KB |
Output is correct |
75 |
Correct |
99 ms |
6764 KB |
Output is correct |
76 |
Correct |
88 ms |
6860 KB |
Output is correct |
77 |
Correct |
85 ms |
6780 KB |
Output is correct |
78 |
Correct |
91 ms |
5604 KB |
Output is correct |
79 |
Correct |
70 ms |
5324 KB |
Output is correct |
80 |
Correct |
102 ms |
5316 KB |
Output is correct |
81 |
Correct |
120 ms |
5356 KB |
Output is correct |
82 |
Correct |
82 ms |
5284 KB |
Output is correct |
83 |
Incorrect |
99 ms |
5600 KB |
Output isn't correct |
84 |
Correct |
89 ms |
5480 KB |
Output is correct |
85 |
Correct |
95 ms |
5452 KB |
Output is correct |
86 |
Correct |
118 ms |
5460 KB |
Output is correct |
87 |
Correct |
103 ms |
5436 KB |
Output is correct |
88 |
Incorrect |
99 ms |
5716 KB |
Output isn't correct |
89 |
Correct |
97 ms |
5572 KB |
Output is correct |
90 |
Correct |
127 ms |
5580 KB |
Output is correct |
91 |
Correct |
144 ms |
5508 KB |
Output is correct |
92 |
Correct |
100 ms |
5532 KB |
Output is correct |