#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;
if(bee[mecho.fi][mecho.se] <= x) return false;
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[mecho.fi][mecho.se] = 'G';
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});
}
}
}
}
a[home.fi][home.se] = 'G';
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 |
2 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 |
2 ms |
4300 KB |
Output is correct |
7 |
Correct |
89 ms |
8340 KB |
Output is correct |
8 |
Correct |
2 ms |
4300 KB |
Output is correct |
9 |
Correct |
2 ms |
4300 KB |
Output is correct |
10 |
Correct |
2 ms |
4300 KB |
Output is correct |
11 |
Correct |
2 ms |
4300 KB |
Output is correct |
12 |
Correct |
2 ms |
4556 KB |
Output is correct |
13 |
Correct |
2 ms |
4428 KB |
Output is correct |
14 |
Correct |
2 ms |
4556 KB |
Output is correct |
15 |
Correct |
2 ms |
4300 KB |
Output is correct |
16 |
Correct |
2 ms |
4300 KB |
Output is correct |
17 |
Correct |
2 ms |
4300 KB |
Output is correct |
18 |
Correct |
2 ms |
4300 KB |
Output is correct |
19 |
Correct |
2 ms |
4428 KB |
Output is correct |
20 |
Correct |
2 ms |
4428 KB |
Output is correct |
21 |
Correct |
2 ms |
4428 KB |
Output is correct |
22 |
Correct |
2 ms |
4428 KB |
Output is correct |
23 |
Correct |
2 ms |
4428 KB |
Output is correct |
24 |
Correct |
2 ms |
4428 KB |
Output is correct |
25 |
Correct |
2 ms |
4556 KB |
Output is correct |
26 |
Correct |
2 ms |
4556 KB |
Output is correct |
27 |
Correct |
2 ms |
4556 KB |
Output is correct |
28 |
Correct |
2 ms |
4556 KB |
Output is correct |
29 |
Correct |
2 ms |
4556 KB |
Output is correct |
30 |
Correct |
2 ms |
4556 KB |
Output is correct |
31 |
Correct |
2 ms |
4556 KB |
Output is correct |
32 |
Correct |
2 ms |
4556 KB |
Output is correct |
33 |
Correct |
8 ms |
5964 KB |
Output is correct |
34 |
Correct |
8 ms |
5916 KB |
Output is correct |
35 |
Correct |
28 ms |
5964 KB |
Output is correct |
36 |
Correct |
8 ms |
6232 KB |
Output is correct |
37 |
Correct |
9 ms |
6276 KB |
Output is correct |
38 |
Correct |
30 ms |
6220 KB |
Output is correct |
39 |
Correct |
16 ms |
6476 KB |
Output is correct |
40 |
Correct |
17 ms |
6512 KB |
Output is correct |
41 |
Correct |
40 ms |
6596 KB |
Output is correct |
42 |
Correct |
11 ms |
6732 KB |
Output is correct |
43 |
Correct |
11 ms |
6760 KB |
Output is correct |
44 |
Correct |
50 ms |
6756 KB |
Output is correct |
45 |
Correct |
15 ms |
6988 KB |
Output is correct |
46 |
Correct |
15 ms |
7012 KB |
Output is correct |
47 |
Correct |
61 ms |
6996 KB |
Output is correct |
48 |
Correct |
16 ms |
7256 KB |
Output is correct |
49 |
Correct |
17 ms |
7252 KB |
Output is correct |
50 |
Correct |
77 ms |
7256 KB |
Output is correct |
51 |
Correct |
20 ms |
7500 KB |
Output is correct |
52 |
Correct |
20 ms |
7500 KB |
Output is correct |
53 |
Correct |
86 ms |
7500 KB |
Output is correct |
54 |
Correct |
22 ms |
7628 KB |
Output is correct |
55 |
Correct |
24 ms |
7752 KB |
Output is correct |
56 |
Correct |
96 ms |
7676 KB |
Output is correct |
57 |
Correct |
23 ms |
7988 KB |
Output is correct |
58 |
Correct |
23 ms |
7920 KB |
Output is correct |
59 |
Correct |
108 ms |
7884 KB |
Output is correct |
60 |
Correct |
25 ms |
8156 KB |
Output is correct |
61 |
Correct |
32 ms |
8232 KB |
Output is correct |
62 |
Correct |
131 ms |
8224 KB |
Output is correct |
63 |
Correct |
105 ms |
8132 KB |
Output is correct |
64 |
Correct |
185 ms |
8260 KB |
Output is correct |
65 |
Correct |
177 ms |
8220 KB |
Output is correct |
66 |
Correct |
131 ms |
8132 KB |
Output is correct |
67 |
Correct |
115 ms |
8144 KB |
Output is correct |
68 |
Correct |
52 ms |
8140 KB |
Output is correct |
69 |
Correct |
57 ms |
8248 KB |
Output is correct |
70 |
Correct |
50 ms |
8248 KB |
Output is correct |
71 |
Correct |
43 ms |
8168 KB |
Output is correct |
72 |
Correct |
43 ms |
8172 KB |
Output is correct |
73 |
Correct |
46 ms |
8432 KB |
Output is correct |
74 |
Correct |
79 ms |
8504 KB |
Output is correct |
75 |
Correct |
70 ms |
8388 KB |
Output is correct |
76 |
Correct |
64 ms |
8420 KB |
Output is correct |
77 |
Correct |
68 ms |
8496 KB |
Output is correct |
78 |
Correct |
77 ms |
8476 KB |
Output is correct |
79 |
Correct |
64 ms |
8516 KB |
Output is correct |
80 |
Correct |
67 ms |
8524 KB |
Output is correct |
81 |
Correct |
69 ms |
8468 KB |
Output is correct |
82 |
Correct |
65 ms |
8472 KB |
Output is correct |
83 |
Correct |
80 ms |
8396 KB |
Output is correct |
84 |
Correct |
76 ms |
8372 KB |
Output is correct |
85 |
Correct |
73 ms |
8356 KB |
Output is correct |
86 |
Correct |
80 ms |
8548 KB |
Output is correct |
87 |
Correct |
80 ms |
8364 KB |
Output is correct |
88 |
Correct |
78 ms |
8380 KB |
Output is correct |
89 |
Correct |
98 ms |
8380 KB |
Output is correct |
90 |
Correct |
89 ms |
8260 KB |
Output is correct |
91 |
Correct |
92 ms |
8396 KB |
Output is correct |
92 |
Correct |
90 ms |
8316 KB |
Output is correct |