#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define mask(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
using namespace std;
const int mxN = 1007;
const ll inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int n , limit;
char a[mxN][mxN];
int bee[mxN][mxN];
int dist[mxN][mxN];
pair<int , int> mecho , home;
int dx[4] = {0 , 1 , 0 , -1};
int dy[4] = {1 , 0 , -1 , 0};
bool ok(int x)
{
for(int i = 0 ; i <= n ; i++)
for(int j = 0 ; j <= n ; j++) dist[i][j] = inf;
queue<pair<int , int>> q;
q.push(mecho);
dist[mecho.fi][mecho.se] = limit * x;
while(!q.empty()) {
int x = q.front().fi;
int y = q.front().se;
q.pop();
for(int d = 0 ; d < 4 ; d++) {
int newx = x + dx[d];
int newy = y + dy[d];
if(1 <= newx && newx <= n && 1 <= newy && newy <= n && a[newx][newy] == 'G') {
if(dist[newx][newy] > dist[x][y] + 1 && bee[newx][newy] > (dist[x][y] + 1) / limit) {
dist[newx][newy] = dist[x][y] + 1;
q.push({newx , newy});
}
}
}
}
if(dist[home.fi][home.se] != dist[0][0]) return true;
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("task.inp" , "r" , stdin);
//freopen("task.out" , "w" , stdout);
cin >> n >> limit;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++) cin >> a[i][j];
memset(bee, 0x3f, sizeof bee);
queue<pair<int , int>> q;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++) {
if(a[i][j] == 'H') {
bee[i][j] = 0;
q.push({i , j});
}
if(a[i][j] == 'M') mecho = {i , j};
if(a[i][j] == 'D') home = {i , j};
}
a[home.fi][home.se] = 'G';
bee[home.fi][home.se] = inf;
while(!q.empty()) {
int x = q.front().fi;
int y = q.front().se;
q.pop();
for(int d = 0 ; d < 4 ; d++) {
int newx = x + dx[d];
int newy = y + dy[d];
if(1 <= newx && newx <= n && 1 <= newy && newy <= n && a[newx][newy] == 'G') {
if(bee[newx][newy] > bee[x][y] + 1) {
bee[newx][newy] = bee[x][y] + 1;
q.push({newx , newy});
}
}
}
}
bee[home.fi][home.se] = inf;
int lo = 0 , hi = inf;
while(lo + 1 < hi) {
int mid = (lo + hi) / 2;
if(ok(mid)) lo = mid;
else hi = mid;
}
if(ok(lo) == false) cout << -1;
else cout << lo;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4300 KB |
Output is correct |
2 |
Correct |
2 ms |
4300 KB |
Output is correct |
3 |
Correct |
2 ms |
4300 KB |
Output is correct |
4 |
Correct |
2 ms |
4300 KB |
Output is correct |
5 |
Correct |
2 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Incorrect |
296 ms |
8332 KB |
Output isn't correct |
8 |
Correct |
2 ms |
4300 KB |
Output is correct |
9 |
Incorrect |
3 ms |
4300 KB |
Output isn't correct |
10 |
Correct |
2 ms |
4300 KB |
Output is correct |
11 |
Correct |
2 ms |
4300 KB |
Output is correct |
12 |
Incorrect |
2 ms |
4556 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
4556 KB |
Output isn't correct |
14 |
Incorrect |
5 ms |
4488 KB |
Output isn't correct |
15 |
Correct |
2 ms |
4300 KB |
Output is correct |
16 |
Incorrect |
2 ms |
4300 KB |
Output isn't correct |
17 |
Correct |
2 ms |
4300 KB |
Output is correct |
18 |
Incorrect |
2 ms |
4300 KB |
Output isn't correct |
19 |
Correct |
2 ms |
4428 KB |
Output is correct |
20 |
Incorrect |
2 ms |
4428 KB |
Output isn't correct |
21 |
Correct |
2 ms |
4428 KB |
Output is correct |
22 |
Incorrect |
2 ms |
4428 KB |
Output isn't correct |
23 |
Correct |
2 ms |
4428 KB |
Output is correct |
24 |
Incorrect |
2 ms |
4428 KB |
Output isn't correct |
25 |
Correct |
2 ms |
4556 KB |
Output is correct |
26 |
Incorrect |
3 ms |
4556 KB |
Output isn't correct |
27 |
Correct |
3 ms |
4596 KB |
Output is correct |
28 |
Incorrect |
2 ms |
4556 KB |
Output isn't correct |
29 |
Correct |
3 ms |
4556 KB |
Output is correct |
30 |
Incorrect |
3 ms |
4556 KB |
Output isn't correct |
31 |
Correct |
3 ms |
4700 KB |
Output is correct |
32 |
Incorrect |
3 ms |
4676 KB |
Output isn't correct |
33 |
Correct |
8 ms |
5964 KB |
Output is correct |
34 |
Incorrect |
22 ms |
6032 KB |
Output isn't correct |
35 |
Incorrect |
42 ms |
6004 KB |
Output isn't correct |
36 |
Correct |
8 ms |
6220 KB |
Output is correct |
37 |
Incorrect |
19 ms |
6272 KB |
Output isn't correct |
38 |
Incorrect |
58 ms |
6256 KB |
Output isn't correct |
39 |
Correct |
10 ms |
6476 KB |
Output is correct |
40 |
Incorrect |
22 ms |
6404 KB |
Output isn't correct |
41 |
Incorrect |
99 ms |
6500 KB |
Output isn't correct |
42 |
Correct |
12 ms |
6732 KB |
Output is correct |
43 |
Incorrect |
29 ms |
6732 KB |
Output isn't correct |
44 |
Incorrect |
110 ms |
6756 KB |
Output isn't correct |
45 |
Correct |
19 ms |
6888 KB |
Output is correct |
46 |
Incorrect |
38 ms |
6972 KB |
Output isn't correct |
47 |
Incorrect |
119 ms |
6992 KB |
Output isn't correct |
48 |
Correct |
17 ms |
7244 KB |
Output is correct |
49 |
Incorrect |
38 ms |
7192 KB |
Output isn't correct |
50 |
Incorrect |
162 ms |
7244 KB |
Output isn't correct |
51 |
Correct |
19 ms |
7500 KB |
Output is correct |
52 |
Incorrect |
37 ms |
7488 KB |
Output isn't correct |
53 |
Incorrect |
178 ms |
7488 KB |
Output isn't correct |
54 |
Correct |
22 ms |
7628 KB |
Output is correct |
55 |
Incorrect |
61 ms |
7740 KB |
Output isn't correct |
56 |
Incorrect |
168 ms |
7732 KB |
Output isn't correct |
57 |
Correct |
33 ms |
8004 KB |
Output is correct |
58 |
Incorrect |
73 ms |
7956 KB |
Output isn't correct |
59 |
Incorrect |
175 ms |
7984 KB |
Output isn't correct |
60 |
Correct |
25 ms |
8152 KB |
Output is correct |
61 |
Incorrect |
68 ms |
8156 KB |
Output isn't correct |
62 |
Incorrect |
238 ms |
8140 KB |
Output isn't correct |
63 |
Correct |
101 ms |
8160 KB |
Output is correct |
64 |
Incorrect |
294 ms |
8152 KB |
Output isn't correct |
65 |
Correct |
169 ms |
8156 KB |
Output is correct |
66 |
Correct |
146 ms |
8224 KB |
Output is correct |
67 |
Correct |
112 ms |
8220 KB |
Output is correct |
68 |
Incorrect |
243 ms |
8184 KB |
Output isn't correct |
69 |
Incorrect |
279 ms |
8248 KB |
Output isn't correct |
70 |
Incorrect |
267 ms |
8172 KB |
Output isn't correct |
71 |
Incorrect |
304 ms |
8132 KB |
Output isn't correct |
72 |
Incorrect |
97 ms |
8276 KB |
Output isn't correct |
73 |
Incorrect |
306 ms |
8492 KB |
Output isn't correct |
74 |
Incorrect |
200 ms |
8504 KB |
Output isn't correct |
75 |
Incorrect |
341 ms |
8500 KB |
Output isn't correct |
76 |
Incorrect |
214 ms |
8504 KB |
Output isn't correct |
77 |
Incorrect |
237 ms |
8500 KB |
Output isn't correct |
78 |
Incorrect |
281 ms |
8644 KB |
Output isn't correct |
79 |
Incorrect |
252 ms |
8396 KB |
Output isn't correct |
80 |
Incorrect |
253 ms |
8396 KB |
Output isn't correct |
81 |
Incorrect |
312 ms |
8376 KB |
Output isn't correct |
82 |
Incorrect |
244 ms |
8472 KB |
Output isn't correct |
83 |
Incorrect |
271 ms |
8516 KB |
Output isn't correct |
84 |
Incorrect |
235 ms |
8428 KB |
Output isn't correct |
85 |
Incorrect |
244 ms |
8432 KB |
Output isn't correct |
86 |
Incorrect |
249 ms |
8500 KB |
Output isn't correct |
87 |
Incorrect |
294 ms |
8432 KB |
Output isn't correct |
88 |
Incorrect |
272 ms |
8388 KB |
Output isn't correct |
89 |
Incorrect |
213 ms |
8380 KB |
Output isn't correct |
90 |
Incorrect |
317 ms |
8388 KB |
Output isn't correct |
91 |
Incorrect |
276 ms |
8412 KB |
Output isn't correct |
92 |
Incorrect |
306 ms |
8316 KB |
Output isn't correct |