#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; vvb vis(n,vb(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][j]=true;
if(F[i][j]=='D') {good=true; break;}
if(tst+d/s>=hd[i][j]) continue;
if(i>0 && !vis[i-1][j] && (F[i-1][j]=='G' || F[i-1][j]=='D')) Qp.push({i-1,j,d+1});
if(i<n-1 && !vis[i+1][j] && (F[i+1][j]=='G' || F[i+1][j]=='D')) Qp.push({i+1,j,d+1});
if(j>0 && !vis[i][j-1] && (F[i][j-1]=='G' || F[i][j-1]=='D')) Qp.push({i,j-1,d+1});
if(j<n-1 && !vis[i][j+1] && (F[i][j+1]=='G' || F[i][j+1]=='D')) Qp.push({i,j+1,d+1});
}
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 |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 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 |
Runtime error |
194 ms |
65540 KB |
Execution killed with signal 9 |
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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Runtime error |
281 ms |
65540 KB |
Execution killed with signal 9 |
14 |
Runtime error |
180 ms |
65540 KB |
Execution killed with signal 9 |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
0 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 |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
6 ms |
972 KB |
Output is correct |
34 |
Correct |
6 ms |
972 KB |
Output is correct |
35 |
Runtime error |
184 ms |
65540 KB |
Execution killed with signal 9 |
36 |
Correct |
6 ms |
1100 KB |
Output is correct |
37 |
Correct |
7 ms |
1100 KB |
Output is correct |
38 |
Runtime error |
208 ms |
65540 KB |
Execution killed with signal 9 |
39 |
Correct |
7 ms |
1356 KB |
Output is correct |
40 |
Correct |
7 ms |
1400 KB |
Output is correct |
41 |
Runtime error |
201 ms |
65540 KB |
Execution killed with signal 9 |
42 |
Correct |
10 ms |
1588 KB |
Output is correct |
43 |
Correct |
11 ms |
1648 KB |
Output is correct |
44 |
Runtime error |
194 ms |
65540 KB |
Execution killed with signal 9 |
45 |
Correct |
10 ms |
1908 KB |
Output is correct |
46 |
Correct |
12 ms |
1848 KB |
Output is correct |
47 |
Runtime error |
198 ms |
65540 KB |
Execution killed with signal 9 |
48 |
Correct |
12 ms |
2148 KB |
Output is correct |
49 |
Correct |
13 ms |
2224 KB |
Output is correct |
50 |
Runtime error |
206 ms |
65540 KB |
Execution killed with signal 9 |
51 |
Correct |
14 ms |
2548 KB |
Output is correct |
52 |
Correct |
14 ms |
2508 KB |
Output is correct |
53 |
Runtime error |
213 ms |
65540 KB |
Execution killed with signal 9 |
54 |
Correct |
21 ms |
2764 KB |
Output is correct |
55 |
Correct |
18 ms |
2884 KB |
Output is correct |
56 |
Runtime error |
203 ms |
65540 KB |
Execution killed with signal 9 |
57 |
Correct |
20 ms |
3264 KB |
Output is correct |
58 |
Correct |
20 ms |
3164 KB |
Output is correct |
59 |
Runtime error |
200 ms |
65540 KB |
Execution killed with signal 9 |
60 |
Correct |
26 ms |
3544 KB |
Output is correct |
61 |
Correct |
22 ms |
3660 KB |
Output is correct |
62 |
Runtime error |
195 ms |
65540 KB |
Execution killed with signal 9 |
63 |
Correct |
106 ms |
3644 KB |
Output is correct |
64 |
Correct |
205 ms |
3652 KB |
Output is correct |
65 |
Correct |
150 ms |
3664 KB |
Output is correct |
66 |
Correct |
128 ms |
3532 KB |
Output is correct |
67 |
Correct |
122 ms |
3544 KB |
Output is correct |
68 |
Correct |
57 ms |
3532 KB |
Output is correct |
69 |
Correct |
55 ms |
3712 KB |
Output is correct |
70 |
Correct |
42 ms |
3716 KB |
Output is correct |
71 |
Correct |
51 ms |
3660 KB |
Output is correct |
72 |
Correct |
39 ms |
3596 KB |
Output is correct |
73 |
Runtime error |
209 ms |
65540 KB |
Execution killed with signal 9 |
74 |
Runtime error |
205 ms |
65540 KB |
Execution killed with signal 9 |
75 |
Runtime error |
191 ms |
65540 KB |
Execution killed with signal 9 |
76 |
Runtime error |
194 ms |
65540 KB |
Execution killed with signal 9 |
77 |
Runtime error |
195 ms |
65540 KB |
Execution killed with signal 9 |
78 |
Runtime error |
200 ms |
65540 KB |
Execution killed with signal 9 |
79 |
Runtime error |
194 ms |
65540 KB |
Execution killed with signal 9 |
80 |
Runtime error |
195 ms |
65540 KB |
Execution killed with signal 9 |
81 |
Runtime error |
218 ms |
65540 KB |
Execution killed with signal 9 |
82 |
Runtime error |
199 ms |
65540 KB |
Execution killed with signal 9 |
83 |
Runtime error |
203 ms |
65540 KB |
Execution killed with signal 9 |
84 |
Runtime error |
238 ms |
65540 KB |
Execution killed with signal 9 |
85 |
Runtime error |
205 ms |
65540 KB |
Execution killed with signal 9 |
86 |
Runtime error |
245 ms |
65540 KB |
Execution killed with signal 9 |
87 |
Runtime error |
199 ms |
65540 KB |
Execution killed with signal 9 |
88 |
Runtime error |
197 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
206 ms |
65540 KB |
Execution killed with signal 9 |
90 |
Runtime error |
205 ms |
65540 KB |
Execution killed with signal 9 |
91 |
Runtime error |
218 ms |
65540 KB |
Execution killed with signal 9 |
92 |
Runtime error |
203 ms |
65540 KB |
Execution killed with signal 9 |