#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvpi = vector<vector<pair<int,int>>>;
using vvb = vector<vector<bool>>;
using str = string;
using pi= pair<int,int>;
using pll = pair<ll,ll>;
using vpi = vector<pi>;
using ti = tuple<int,int,int>;
using vti = vector<ti>;
#define pb push_back
#define rs resize
#define mp make_pair
#define FOR(x,y,z) for(int x=y; x<z; x++)
#define F0R(x,z) for(int x=0; x<z; x++)
#define all(a) a.begin() , a.end()
ll mx = 1000000000000000000;
int inf = 2000000000;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n,s; cin >> n >> s;
vector<str> F(n);
for(int i=0;i<n;i++) cin >> F[i];
vvi hd(n,vi(n,inf));
queue<ti> Q;
for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(F[i][j]=='H') Q.push({i,j,0});
while(!Q.empty()) {
ti cur=Q.front(); Q.pop();
int i=get<0>(cur),j=get<1>(cur),d=get<2>(cur);
//cout << i << j << d << "\n";
if(d>=hd[i][j]) continue;
hd[i][j]=d;
if(i>0 && hd[i-1][j]==inf && (F[i-1][j]=='G' || F[i-1][j]=='M')) Q.push({i-1,j,d+1});
if(i<n-1 && hd[i+1][j]==inf && (F[i+1][j]=='G' || F[i+1][j]=='M')) Q.push({i+1,j,d+1});
if(j>0 && hd[i][j-1]==inf && (F[i][j-1]=='G' || F[i][j-1]=='M')) Q.push({i,j-1,d+1});
if(j<n-1 && hd[i][j+1]==inf && (F[i][j+1]=='G' || F[i][j+1]=='M')) Q.push({i,j+1,d+1});
}
//for(int i=0;i<n;i++) {for(int j=0;j<n;j++) if(hd[i][j]<inf) cout << hd[i][j]; else cout << "T"; cout << "\n";}
int lob=-1,upb=n*n;
while(lob<upb) {
int tst=(lob+upb+1)/2;
queue<ti> Qp;
for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(F[i][j]=='M') Qp.push({i,j,0});
bool good=false; vb vis(n*n,false);
while(!Qp.empty()) {
ti cur=Qp.front(); Qp.pop();
int i=get<0>(cur),j=get<1>(cur),d=get<2>(cur);
//cout << i << j << d << " ";
vis[i*n+j]=true;
if(F[i][j]=='D') {good=true; break;}
if(tst+d/s>=hd[i][j]) continue;
if(i>0 && !vis[(i-1)*n+j] && (F[i-1][j]=='G' || F[i-1][j]=='D')) {Qp.push({i-1,j,d+1}); vis[n*(i-1)+j]=true;}
if(i<n-1 && !vis[n*(i+1)+j] && (F[i+1][j]=='G' || F[i+1][j]=='D')) {Qp.push({i+1,j,d+1}); vis[n*(i+1)+j]=true;}
if(j>0 && !vis[n*i+j-1] && (F[i][j-1]=='G' || F[i][j-1]=='D')) {Qp.push({i,j-1,d+1}); vis[n*i+j-1]=true;}
if(j<n-1 && !vis[n*i+j+1] && (F[i][j+1]=='G' || F[i][j+1]=='D')) {Qp.push({i,j+1,d+1}); vis[n*i+j+1]=true;}
}
if(!good) upb=tst-1;
else lob=tst;
}
cout << upb;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
95 ms |
3856 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
4 ms |
956 KB |
Output is correct |
34 |
Correct |
4 ms |
844 KB |
Output is correct |
35 |
Correct |
23 ms |
956 KB |
Output is correct |
36 |
Correct |
5 ms |
1100 KB |
Output is correct |
37 |
Correct |
5 ms |
1152 KB |
Output is correct |
38 |
Correct |
26 ms |
1100 KB |
Output is correct |
39 |
Correct |
6 ms |
1356 KB |
Output is correct |
40 |
Correct |
5 ms |
1356 KB |
Output is correct |
41 |
Correct |
35 ms |
1356 KB |
Output is correct |
42 |
Correct |
7 ms |
1612 KB |
Output is correct |
43 |
Correct |
8 ms |
1584 KB |
Output is correct |
44 |
Correct |
47 ms |
1596 KB |
Output is correct |
45 |
Correct |
9 ms |
1868 KB |
Output is correct |
46 |
Correct |
13 ms |
1876 KB |
Output is correct |
47 |
Correct |
49 ms |
1868 KB |
Output is correct |
48 |
Correct |
11 ms |
2192 KB |
Output is correct |
49 |
Correct |
14 ms |
2220 KB |
Output is correct |
50 |
Correct |
61 ms |
2124 KB |
Output is correct |
51 |
Correct |
13 ms |
2508 KB |
Output is correct |
52 |
Correct |
12 ms |
2508 KB |
Output is correct |
53 |
Correct |
82 ms |
2496 KB |
Output is correct |
54 |
Correct |
15 ms |
2856 KB |
Output is correct |
55 |
Correct |
16 ms |
2764 KB |
Output is correct |
56 |
Correct |
104 ms |
2844 KB |
Output is correct |
57 |
Correct |
17 ms |
3232 KB |
Output is correct |
58 |
Correct |
21 ms |
3276 KB |
Output is correct |
59 |
Correct |
134 ms |
3216 KB |
Output is correct |
60 |
Correct |
19 ms |
3532 KB |
Output is correct |
61 |
Correct |
18 ms |
3532 KB |
Output is correct |
62 |
Correct |
129 ms |
3556 KB |
Output is correct |
63 |
Correct |
113 ms |
3540 KB |
Output is correct |
64 |
Correct |
213 ms |
3652 KB |
Output is correct |
65 |
Correct |
156 ms |
3604 KB |
Output is correct |
66 |
Correct |
127 ms |
3492 KB |
Output is correct |
67 |
Correct |
126 ms |
3612 KB |
Output is correct |
68 |
Correct |
50 ms |
3572 KB |
Output is correct |
69 |
Correct |
54 ms |
3576 KB |
Output is correct |
70 |
Correct |
38 ms |
3588 KB |
Output is correct |
71 |
Correct |
44 ms |
3644 KB |
Output is correct |
72 |
Correct |
34 ms |
3644 KB |
Output is correct |
73 |
Correct |
43 ms |
4300 KB |
Output is correct |
74 |
Correct |
67 ms |
4300 KB |
Output is correct |
75 |
Correct |
86 ms |
4352 KB |
Output is correct |
76 |
Correct |
78 ms |
4352 KB |
Output is correct |
77 |
Correct |
77 ms |
4296 KB |
Output is correct |
78 |
Correct |
84 ms |
4172 KB |
Output is correct |
79 |
Correct |
64 ms |
4172 KB |
Output is correct |
80 |
Correct |
68 ms |
4172 KB |
Output is correct |
81 |
Correct |
78 ms |
4268 KB |
Output is correct |
82 |
Correct |
81 ms |
4292 KB |
Output is correct |
83 |
Correct |
89 ms |
4140 KB |
Output is correct |
84 |
Correct |
76 ms |
4136 KB |
Output is correct |
85 |
Correct |
89 ms |
4044 KB |
Output is correct |
86 |
Correct |
80 ms |
4116 KB |
Output is correct |
87 |
Correct |
84 ms |
4156 KB |
Output is correct |
88 |
Correct |
106 ms |
3916 KB |
Output is correct |
89 |
Correct |
115 ms |
4000 KB |
Output is correct |
90 |
Correct |
93 ms |
3940 KB |
Output is correct |
91 |
Correct |
89 ms |
4004 KB |
Output is correct |
92 |
Correct |
95 ms |
3916 KB |
Output is correct |