#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 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
90 ms |
6724 KB |
Output is correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Incorrect |
1 ms |
720 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
18 |
Correct |
1 ms |
516 KB |
Output is correct |
19 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
21 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
28 |
Correct |
1 ms |
732 KB |
Output is correct |
29 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
30 |
Correct |
1 ms |
732 KB |
Output is correct |
31 |
Incorrect |
1 ms |
728 KB |
Output isn't correct |
32 |
Correct |
1 ms |
720 KB |
Output is correct |
33 |
Incorrect |
5 ms |
2784 KB |
Output isn't correct |
34 |
Correct |
5 ms |
2780 KB |
Output is correct |
35 |
Correct |
27 ms |
2796 KB |
Output is correct |
36 |
Incorrect |
7 ms |
3156 KB |
Output isn't correct |
37 |
Correct |
6 ms |
3156 KB |
Output is correct |
38 |
Correct |
26 ms |
3196 KB |
Output is correct |
39 |
Incorrect |
9 ms |
3580 KB |
Output isn't correct |
40 |
Correct |
7 ms |
3572 KB |
Output is correct |
41 |
Correct |
33 ms |
3524 KB |
Output is correct |
42 |
Incorrect |
9 ms |
3916 KB |
Output isn't correct |
43 |
Correct |
9 ms |
4020 KB |
Output is correct |
44 |
Correct |
47 ms |
4008 KB |
Output is correct |
45 |
Incorrect |
11 ms |
4292 KB |
Output isn't correct |
46 |
Correct |
10 ms |
4428 KB |
Output is correct |
47 |
Correct |
49 ms |
4428 KB |
Output is correct |
48 |
Incorrect |
11 ms |
4812 KB |
Output isn't correct |
49 |
Correct |
11 ms |
4812 KB |
Output is correct |
50 |
Correct |
63 ms |
4812 KB |
Output is correct |
51 |
Incorrect |
15 ms |
5196 KB |
Output isn't correct |
52 |
Correct |
13 ms |
5196 KB |
Output is correct |
53 |
Correct |
71 ms |
5312 KB |
Output is correct |
54 |
Incorrect |
15 ms |
5708 KB |
Output isn't correct |
55 |
Correct |
15 ms |
5764 KB |
Output is correct |
56 |
Correct |
82 ms |
5708 KB |
Output is correct |
57 |
Incorrect |
17 ms |
6216 KB |
Output isn't correct |
58 |
Correct |
17 ms |
6196 KB |
Output is correct |
59 |
Correct |
108 ms |
6220 KB |
Output is correct |
60 |
Incorrect |
20 ms |
6624 KB |
Output isn't correct |
61 |
Correct |
20 ms |
6580 KB |
Output is correct |
62 |
Correct |
114 ms |
6636 KB |
Output is correct |
63 |
Correct |
109 ms |
6684 KB |
Output is correct |
64 |
Correct |
193 ms |
6624 KB |
Output is correct |
65 |
Correct |
178 ms |
6680 KB |
Output is correct |
66 |
Incorrect |
134 ms |
6588 KB |
Output isn't correct |
67 |
Correct |
31 ms |
6596 KB |
Output is correct |
68 |
Correct |
52 ms |
6632 KB |
Output is correct |
69 |
Correct |
51 ms |
6724 KB |
Output is correct |
70 |
Correct |
48 ms |
6640 KB |
Output is correct |
71 |
Correct |
45 ms |
6704 KB |
Output is correct |
72 |
Incorrect |
37 ms |
6600 KB |
Output isn't correct |
73 |
Incorrect |
40 ms |
6868 KB |
Output isn't correct |
74 |
Correct |
63 ms |
6896 KB |
Output is correct |
75 |
Correct |
65 ms |
6880 KB |
Output is correct |
76 |
Correct |
65 ms |
7024 KB |
Output is correct |
77 |
Correct |
66 ms |
6860 KB |
Output is correct |
78 |
Correct |
26 ms |
6936 KB |
Output is correct |
79 |
Correct |
66 ms |
6860 KB |
Output is correct |
80 |
Correct |
61 ms |
6852 KB |
Output is correct |
81 |
Correct |
69 ms |
6932 KB |
Output is correct |
82 |
Correct |
63 ms |
6876 KB |
Output is correct |
83 |
Correct |
77 ms |
6892 KB |
Output is correct |
84 |
Correct |
79 ms |
6892 KB |
Output is correct |
85 |
Correct |
68 ms |
6876 KB |
Output is correct |
86 |
Correct |
87 ms |
6892 KB |
Output is correct |
87 |
Correct |
82 ms |
6888 KB |
Output is correct |
88 |
Correct |
77 ms |
6776 KB |
Output is correct |
89 |
Correct |
96 ms |
6848 KB |
Output is correct |
90 |
Correct |
88 ms |
6764 KB |
Output is correct |
91 |
Correct |
85 ms |
6848 KB |
Output is correct |
92 |
Correct |
79 ms |
6840 KB |
Output is correct |