#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=807, INF=1e9+7;
int dx[]={1, 0, -1, 0}, dy[]={0, 1, 0, -1};
string T[LIM];
int odl[LIM][LIM], wolne[LIM][LIM], n, s;
bool pole(int a, int b) {
return 0<=a && a<n && 0<=b && b<n;
}
bool ok(int x) {
queue<pair<int,int>>q;
rep(i, n) rep(j, n) {
if(T[i][j]=='M') {
q.push({i, j});
odl[i][j]=x*s;
} else odl[i][j]=INF;
}
while(!q.empty()) {
int a=q.front().st, b=q.front().nd; q.pop();
if(odl[a][b]>=wolne[a][b]) continue;
if(T[a][b]=='D') return true;
rep(i, 4) if(pole(a+dx[i], b+dy[i])) {
if((T[a+dx[i]][b+dy[i]]=='G' || T[a+dx[i]][b+dy[i]]=='D') && odl[a+dx[i]][b+dy[i]]==INF) {
odl[a+dx[i]][b+dy[i]]=odl[a][b]+1;
q.push({a+dx[i], b+dy[i]});
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> s;
queue<pair<int,int>>q;
rep(i, n) {
cin >> T[i];
rep(j, n) {
if(T[i][j]=='H') q.push({i, j});
else wolne[i][j]=INF;
}
}
while(!q.empty()) {
int a=q.front().st, b=q.front().nd; q.pop();
rep(i, 4) if(pole(a+dx[i], b+dy[i])) {
if((T[a+dx[i]][b+dy[i]]=='G' || T[a+dx[i]][b+dy[i]]=='M') && wolne[a+dx[i]][b+dy[i]]==INF) {
wolne[a+dx[i]][b+dy[i]]=wolne[a][b]+s;
q.push({a+dx[i], b+dy[i]});
}
}
}
if(!ok(0)) {
cout << -1 << '\n';
return 0;
}
int p=0, k=LIM*LIM;
while(p<k) {
int sr=(p+k+1)/2;
if(ok(sr)) p=sr; else k=sr-1;
}
cout << p << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
82 ms |
6168 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
716 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
716 KB |
Output is correct |
32 |
Correct |
1 ms |
716 KB |
Output is correct |
33 |
Correct |
5 ms |
2636 KB |
Output is correct |
34 |
Correct |
7 ms |
2604 KB |
Output is correct |
35 |
Correct |
21 ms |
2636 KB |
Output is correct |
36 |
Correct |
6 ms |
3048 KB |
Output is correct |
37 |
Correct |
5 ms |
3044 KB |
Output is correct |
38 |
Correct |
25 ms |
3020 KB |
Output is correct |
39 |
Correct |
7 ms |
3404 KB |
Output is correct |
40 |
Correct |
7 ms |
3404 KB |
Output is correct |
41 |
Correct |
35 ms |
3384 KB |
Output is correct |
42 |
Correct |
8 ms |
3660 KB |
Output is correct |
43 |
Correct |
12 ms |
3660 KB |
Output is correct |
44 |
Correct |
42 ms |
3660 KB |
Output is correct |
45 |
Correct |
10 ms |
4060 KB |
Output is correct |
46 |
Correct |
10 ms |
4044 KB |
Output is correct |
47 |
Correct |
48 ms |
4044 KB |
Output is correct |
48 |
Correct |
11 ms |
4428 KB |
Output is correct |
49 |
Correct |
11 ms |
4428 KB |
Output is correct |
50 |
Correct |
55 ms |
4424 KB |
Output is correct |
51 |
Correct |
18 ms |
4904 KB |
Output is correct |
52 |
Correct |
18 ms |
4904 KB |
Output is correct |
53 |
Correct |
75 ms |
4904 KB |
Output is correct |
54 |
Correct |
16 ms |
5192 KB |
Output is correct |
55 |
Correct |
14 ms |
5284 KB |
Output is correct |
56 |
Correct |
82 ms |
5280 KB |
Output is correct |
57 |
Correct |
22 ms |
5580 KB |
Output is correct |
58 |
Correct |
18 ms |
5684 KB |
Output is correct |
59 |
Correct |
112 ms |
5672 KB |
Output is correct |
60 |
Correct |
20 ms |
6068 KB |
Output is correct |
61 |
Correct |
20 ms |
5964 KB |
Output is correct |
62 |
Correct |
125 ms |
6060 KB |
Output is correct |
63 |
Correct |
118 ms |
5984 KB |
Output is correct |
64 |
Correct |
188 ms |
5980 KB |
Output is correct |
65 |
Correct |
183 ms |
6056 KB |
Output is correct |
66 |
Correct |
139 ms |
6048 KB |
Output is correct |
67 |
Correct |
33 ms |
5964 KB |
Output is correct |
68 |
Correct |
52 ms |
6004 KB |
Output is correct |
69 |
Correct |
51 ms |
6000 KB |
Output is correct |
70 |
Correct |
50 ms |
6092 KB |
Output is correct |
71 |
Correct |
47 ms |
6076 KB |
Output is correct |
72 |
Correct |
45 ms |
5984 KB |
Output is correct |
73 |
Correct |
37 ms |
6468 KB |
Output is correct |
74 |
Correct |
62 ms |
6604 KB |
Output is correct |
75 |
Correct |
66 ms |
6604 KB |
Output is correct |
76 |
Correct |
67 ms |
6596 KB |
Output is correct |
77 |
Correct |
73 ms |
6596 KB |
Output is correct |
78 |
Correct |
36 ms |
6464 KB |
Output is correct |
79 |
Correct |
56 ms |
6560 KB |
Output is correct |
80 |
Correct |
59 ms |
6476 KB |
Output is correct |
81 |
Correct |
68 ms |
6488 KB |
Output is correct |
82 |
Correct |
68 ms |
6604 KB |
Output is correct |
83 |
Correct |
78 ms |
6468 KB |
Output is correct |
84 |
Correct |
84 ms |
6524 KB |
Output is correct |
85 |
Correct |
70 ms |
6452 KB |
Output is correct |
86 |
Correct |
85 ms |
6476 KB |
Output is correct |
87 |
Correct |
79 ms |
6520 KB |
Output is correct |
88 |
Correct |
81 ms |
6472 KB |
Output is correct |
89 |
Correct |
94 ms |
6388 KB |
Output is correct |
90 |
Correct |
89 ms |
6468 KB |
Output is correct |
91 |
Correct |
88 ms |
6468 KB |
Output is correct |
92 |
Correct |
76 ms |
6476 KB |
Output is correct |