#include <bits/stdc++.h>
using namespace std;
bool visited[805][805];
int distBee[805][805];
double distBear[805][805];
char g[805][805];
int dr[] = {-1,0,1,0};
int dc[] = {0,1,0,-1};
int n,sr,sc,er,ec,mxtime;
double s;
bool success;
bool possible(double t) {
memset(visited,0,sizeof visited);
queue < pair<int,int> > bfs;
distBear[sr][sc] = t;
bfs.push({sr,sc});
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]=='T'||distBear[curR][curC]+(double)1/s>=(double)distBee[nr][nc]) continue;
visited[nr][nc] = true;
distBear[nr][nc] = distBear[curR][curC]+1/s;
bfs.push({nr,nc});
}
}
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];
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;
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==sr&&nc==sc) distBee[nr][nc] = distBee[curR][curC]+1;
if (nr<1||nr>n||nc<1||nc>n||visited[nr][nc]||g[nr][nc]=='T'||g[nr][nc]=='M') 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 = distBee[sr][sc]-1;
while (lo<hi) {
int mid = (lo+hi+1)/2;
if (possible((double)mid)) lo = mid;
else hi = mid-1;
}
if (!possible(0)) cout << -1/0 << endl;
else cout << lo << endl;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:71:33: warning: division by zero [-Wdiv-by-zero]
71 | if (!possible(0)) cout << -1/0 << endl;
| ~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Runtime error |
2 ms |
1748 KB |
Execution killed with signal 8 |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Runtime error |
2 ms |
1876 KB |
Execution killed with signal 8 |
7 |
Incorrect |
120 ms |
9140 KB |
Output isn't correct |
8 |
Runtime error |
2 ms |
1876 KB |
Execution killed with signal 8 |
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 |
Runtime error |
2 ms |
2644 KB |
Execution killed with signal 8 |
13 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
14 |
Runtime error |
3 ms |
2388 KB |
Execution killed with signal 8 |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1492 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
2 ms |
1492 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1492 KB |
Output is correct |
33 |
Correct |
11 ms |
3168 KB |
Output is correct |
34 |
Correct |
15 ms |
4272 KB |
Output is correct |
35 |
Correct |
29 ms |
4516 KB |
Output is correct |
36 |
Correct |
15 ms |
3540 KB |
Output is correct |
37 |
Correct |
16 ms |
4828 KB |
Output is correct |
38 |
Correct |
35 ms |
4928 KB |
Output is correct |
39 |
Correct |
18 ms |
3820 KB |
Output is correct |
40 |
Correct |
20 ms |
5324 KB |
Output is correct |
41 |
Correct |
47 ms |
5452 KB |
Output is correct |
42 |
Correct |
22 ms |
4200 KB |
Output is correct |
43 |
Correct |
25 ms |
5932 KB |
Output is correct |
44 |
Correct |
65 ms |
6160 KB |
Output is correct |
45 |
Correct |
26 ms |
4440 KB |
Output is correct |
46 |
Correct |
28 ms |
6420 KB |
Output is correct |
47 |
Correct |
65 ms |
6460 KB |
Output is correct |
48 |
Correct |
30 ms |
4808 KB |
Output is correct |
49 |
Correct |
33 ms |
6964 KB |
Output is correct |
50 |
Correct |
79 ms |
6960 KB |
Output is correct |
51 |
Correct |
36 ms |
5104 KB |
Output is correct |
52 |
Correct |
38 ms |
7316 KB |
Output is correct |
53 |
Correct |
104 ms |
7452 KB |
Output is correct |
54 |
Correct |
41 ms |
5384 KB |
Output is correct |
55 |
Correct |
45 ms |
7756 KB |
Output is correct |
56 |
Correct |
122 ms |
8052 KB |
Output is correct |
57 |
Correct |
47 ms |
5820 KB |
Output is correct |
58 |
Correct |
54 ms |
8140 KB |
Output is correct |
59 |
Correct |
141 ms |
8480 KB |
Output is correct |
60 |
Correct |
52 ms |
6024 KB |
Output is correct |
61 |
Correct |
57 ms |
8608 KB |
Output is correct |
62 |
Correct |
166 ms |
9096 KB |
Output is correct |
63 |
Correct |
128 ms |
8936 KB |
Output is correct |
64 |
Correct |
189 ms |
9108 KB |
Output is correct |
65 |
Correct |
194 ms |
9056 KB |
Output is correct |
66 |
Correct |
182 ms |
9120 KB |
Output is correct |
67 |
Runtime error |
153 ms |
17860 KB |
Execution killed with signal 8 |
68 |
Correct |
75 ms |
8572 KB |
Output is correct |
69 |
Correct |
69 ms |
8836 KB |
Output is correct |
70 |
Correct |
67 ms |
8456 KB |
Output is correct |
71 |
Correct |
69 ms |
8632 KB |
Output is correct |
72 |
Runtime error |
101 ms |
17932 KB |
Execution killed with signal 8 |
73 |
Incorrect |
94 ms |
9304 KB |
Output isn't correct |
74 |
Correct |
97 ms |
9344 KB |
Output is correct |
75 |
Correct |
102 ms |
9292 KB |
Output is correct |
76 |
Correct |
89 ms |
9152 KB |
Output is correct |
77 |
Correct |
94 ms |
9344 KB |
Output is correct |
78 |
Runtime error |
108 ms |
13516 KB |
Execution killed with signal 8 |
79 |
Correct |
94 ms |
9324 KB |
Output is correct |
80 |
Correct |
91 ms |
6728 KB |
Output is correct |
81 |
Correct |
100 ms |
6732 KB |
Output is correct |
82 |
Correct |
91 ms |
6876 KB |
Output is correct |
83 |
Correct |
98 ms |
6696 KB |
Output is correct |
84 |
Correct |
107 ms |
9248 KB |
Output is correct |
85 |
Correct |
94 ms |
6752 KB |
Output is correct |
86 |
Correct |
107 ms |
6728 KB |
Output is correct |
87 |
Correct |
103 ms |
7052 KB |
Output is correct |
88 |
Correct |
107 ms |
7048 KB |
Output is correct |
89 |
Correct |
113 ms |
9188 KB |
Output is correct |
90 |
Correct |
116 ms |
8316 KB |
Output is correct |
91 |
Correct |
125 ms |
9296 KB |
Output is correct |
92 |
Correct |
115 ms |
7148 KB |
Output is correct |