#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';
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});
}
}
}
}
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 |
Incorrect |
2 ms |
4292 KB |
Output isn't correct |
3 |
Correct |
2 ms |
4300 KB |
Output is correct |
4 |
Incorrect |
2 ms |
4300 KB |
Output isn't correct |
5 |
Correct |
2 ms |
4300 KB |
Output is correct |
6 |
Incorrect |
2 ms |
4300 KB |
Output isn't correct |
7 |
Incorrect |
287 ms |
8964 KB |
Output isn't correct |
8 |
Correct |
2 ms |
4300 KB |
Output is correct |
9 |
Incorrect |
2 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 |
4 ms |
4556 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 |
3 ms |
4300 KB |
Output is correct |
18 |
Incorrect |
2 ms |
4416 KB |
Output isn't correct |
19 |
Correct |
3 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 |
2 ms |
4556 KB |
Output isn't correct |
27 |
Correct |
2 ms |
4556 KB |
Output is correct |
28 |
Incorrect |
3 ms |
4556 KB |
Output isn't correct |
29 |
Correct |
2 ms |
4556 KB |
Output is correct |
30 |
Incorrect |
3 ms |
4556 KB |
Output isn't correct |
31 |
Correct |
3 ms |
4556 KB |
Output is correct |
32 |
Incorrect |
3 ms |
4556 KB |
Output isn't correct |
33 |
Correct |
8 ms |
6092 KB |
Output is correct |
34 |
Incorrect |
18 ms |
6092 KB |
Output isn't correct |
35 |
Incorrect |
49 ms |
6124 KB |
Output isn't correct |
36 |
Correct |
9 ms |
6348 KB |
Output is correct |
37 |
Incorrect |
18 ms |
6348 KB |
Output isn't correct |
38 |
Incorrect |
64 ms |
6348 KB |
Output isn't correct |
39 |
Correct |
11 ms |
6604 KB |
Output is correct |
40 |
Incorrect |
29 ms |
6700 KB |
Output isn't correct |
41 |
Incorrect |
109 ms |
6604 KB |
Output isn't correct |
42 |
Correct |
12 ms |
6988 KB |
Output is correct |
43 |
Incorrect |
32 ms |
6992 KB |
Output isn't correct |
44 |
Incorrect |
114 ms |
6988 KB |
Output isn't correct |
45 |
Correct |
15 ms |
7204 KB |
Output is correct |
46 |
Incorrect |
35 ms |
7244 KB |
Output isn't correct |
47 |
Incorrect |
117 ms |
7296 KB |
Output isn't correct |
48 |
Correct |
18 ms |
7612 KB |
Output is correct |
49 |
Incorrect |
55 ms |
7544 KB |
Output isn't correct |
50 |
Incorrect |
158 ms |
7556 KB |
Output isn't correct |
51 |
Correct |
22 ms |
7876 KB |
Output is correct |
52 |
Incorrect |
49 ms |
7828 KB |
Output isn't correct |
53 |
Incorrect |
222 ms |
7920 KB |
Output isn't correct |
54 |
Correct |
33 ms |
8152 KB |
Output is correct |
55 |
Incorrect |
49 ms |
8140 KB |
Output isn't correct |
56 |
Incorrect |
177 ms |
8216 KB |
Output isn't correct |
57 |
Correct |
28 ms |
8532 KB |
Output is correct |
58 |
Incorrect |
63 ms |
8524 KB |
Output isn't correct |
59 |
Incorrect |
192 ms |
8524 KB |
Output isn't correct |
60 |
Correct |
31 ms |
8772 KB |
Output is correct |
61 |
Incorrect |
68 ms |
8780 KB |
Output isn't correct |
62 |
Incorrect |
251 ms |
8856 KB |
Output isn't correct |
63 |
Correct |
107 ms |
8848 KB |
Output is correct |
64 |
Incorrect |
298 ms |
8848 KB |
Output isn't correct |
65 |
Correct |
210 ms |
8944 KB |
Output is correct |
66 |
Correct |
121 ms |
8784 KB |
Output is correct |
67 |
Correct |
111 ms |
8732 KB |
Output is correct |
68 |
Incorrect |
272 ms |
8880 KB |
Output isn't correct |
69 |
Incorrect |
264 ms |
8872 KB |
Output isn't correct |
70 |
Incorrect |
265 ms |
8880 KB |
Output isn't correct |
71 |
Incorrect |
234 ms |
8900 KB |
Output isn't correct |
72 |
Incorrect |
89 ms |
8872 KB |
Output isn't correct |
73 |
Incorrect |
270 ms |
9132 KB |
Output isn't correct |
74 |
Incorrect |
215 ms |
9108 KB |
Output isn't correct |
75 |
Incorrect |
337 ms |
9128 KB |
Output isn't correct |
76 |
Incorrect |
227 ms |
9132 KB |
Output isn't correct |
77 |
Incorrect |
231 ms |
9184 KB |
Output isn't correct |
78 |
Incorrect |
265 ms |
9160 KB |
Output isn't correct |
79 |
Incorrect |
256 ms |
9096 KB |
Output isn't correct |
80 |
Incorrect |
252 ms |
9096 KB |
Output isn't correct |
81 |
Incorrect |
281 ms |
9044 KB |
Output isn't correct |
82 |
Incorrect |
250 ms |
9100 KB |
Output isn't correct |
83 |
Incorrect |
263 ms |
8988 KB |
Output isn't correct |
84 |
Incorrect |
238 ms |
9056 KB |
Output isn't correct |
85 |
Incorrect |
236 ms |
8996 KB |
Output isn't correct |
86 |
Incorrect |
244 ms |
9120 KB |
Output isn't correct |
87 |
Incorrect |
299 ms |
9060 KB |
Output isn't correct |
88 |
Incorrect |
252 ms |
8900 KB |
Output isn't correct |
89 |
Incorrect |
226 ms |
8908 KB |
Output isn't correct |
90 |
Incorrect |
329 ms |
9012 KB |
Output isn't correct |
91 |
Incorrect |
283 ms |
9028 KB |
Output isn't correct |
92 |
Incorrect |
288 ms |
8900 KB |
Output isn't correct |