#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 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]) 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;
bool poss = possible(mid);
cout << mid << ' ' << poss << endl;
if (poss) lo = mid;
else hi = mid-1;
}
if (!possible(0)) cout << -1 << endl;
else cout << lo << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
7 |
Incorrect |
110 ms |
6632 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
1364 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
1064 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
1364 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
1244 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
1364 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
1256 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
1364 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
1364 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
1364 KB |
Output isn't correct |
33 |
Incorrect |
13 ms |
2792 KB |
Output isn't correct |
34 |
Incorrect |
13 ms |
3348 KB |
Output isn't correct |
35 |
Incorrect |
27 ms |
3432 KB |
Output isn't correct |
36 |
Incorrect |
14 ms |
3156 KB |
Output isn't correct |
37 |
Incorrect |
17 ms |
3676 KB |
Output isn't correct |
38 |
Incorrect |
34 ms |
3712 KB |
Output isn't correct |
39 |
Incorrect |
18 ms |
3364 KB |
Output isn't correct |
40 |
Incorrect |
20 ms |
4160 KB |
Output isn't correct |
41 |
Incorrect |
49 ms |
4148 KB |
Output isn't correct |
42 |
Incorrect |
22 ms |
3568 KB |
Output isn't correct |
43 |
Incorrect |
24 ms |
4416 KB |
Output isn't correct |
44 |
Incorrect |
56 ms |
4480 KB |
Output isn't correct |
45 |
Incorrect |
25 ms |
3872 KB |
Output isn't correct |
46 |
Incorrect |
33 ms |
4764 KB |
Output isn't correct |
47 |
Incorrect |
69 ms |
4832 KB |
Output isn't correct |
48 |
Incorrect |
36 ms |
4156 KB |
Output isn't correct |
49 |
Incorrect |
35 ms |
5204 KB |
Output isn't correct |
50 |
Incorrect |
87 ms |
5068 KB |
Output isn't correct |
51 |
Incorrect |
34 ms |
4508 KB |
Output isn't correct |
52 |
Incorrect |
39 ms |
5516 KB |
Output isn't correct |
53 |
Incorrect |
109 ms |
5552 KB |
Output isn't correct |
54 |
Incorrect |
42 ms |
4712 KB |
Output isn't correct |
55 |
Incorrect |
43 ms |
5888 KB |
Output isn't correct |
56 |
Incorrect |
125 ms |
5896 KB |
Output isn't correct |
57 |
Incorrect |
48 ms |
5044 KB |
Output isn't correct |
58 |
Incorrect |
54 ms |
6176 KB |
Output isn't correct |
59 |
Incorrect |
131 ms |
6148 KB |
Output isn't correct |
60 |
Incorrect |
51 ms |
5276 KB |
Output isn't correct |
61 |
Incorrect |
56 ms |
6516 KB |
Output isn't correct |
62 |
Incorrect |
178 ms |
6612 KB |
Output isn't correct |
63 |
Incorrect |
144 ms |
6512 KB |
Output isn't correct |
64 |
Incorrect |
193 ms |
6516 KB |
Output isn't correct |
65 |
Incorrect |
200 ms |
6604 KB |
Output isn't correct |
66 |
Incorrect |
148 ms |
6604 KB |
Output isn't correct |
67 |
Incorrect |
148 ms |
6692 KB |
Output isn't correct |
68 |
Incorrect |
80 ms |
6540 KB |
Output isn't correct |
69 |
Incorrect |
76 ms |
6520 KB |
Output isn't correct |
70 |
Incorrect |
63 ms |
6608 KB |
Output isn't correct |
71 |
Incorrect |
64 ms |
6536 KB |
Output isn't correct |
72 |
Incorrect |
63 ms |
6588 KB |
Output isn't correct |
73 |
Incorrect |
112 ms |
6744 KB |
Output isn't correct |
74 |
Incorrect |
94 ms |
6868 KB |
Output isn't correct |
75 |
Incorrect |
91 ms |
6928 KB |
Output isn't correct |
76 |
Incorrect |
102 ms |
6832 KB |
Output isn't correct |
77 |
Incorrect |
115 ms |
6768 KB |
Output isn't correct |
78 |
Incorrect |
95 ms |
5544 KB |
Output isn't correct |
79 |
Incorrect |
90 ms |
6760 KB |
Output isn't correct |
80 |
Incorrect |
85 ms |
5644 KB |
Output isn't correct |
81 |
Incorrect |
107 ms |
5504 KB |
Output isn't correct |
82 |
Incorrect |
85 ms |
5544 KB |
Output isn't correct |
83 |
Incorrect |
101 ms |
5496 KB |
Output isn't correct |
84 |
Incorrect |
108 ms |
6768 KB |
Output isn't correct |
85 |
Incorrect |
116 ms |
5520 KB |
Output isn't correct |
86 |
Incorrect |
128 ms |
5556 KB |
Output isn't correct |
87 |
Incorrect |
98 ms |
5660 KB |
Output isn't correct |
88 |
Incorrect |
102 ms |
5616 KB |
Output isn't correct |
89 |
Incorrect |
111 ms |
6648 KB |
Output isn't correct |
90 |
Incorrect |
128 ms |
6392 KB |
Output isn't correct |
91 |
Incorrect |
114 ms |
6664 KB |
Output isn't correct |
92 |
Incorrect |
106 ms |
5748 KB |
Output isn't correct |