#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forn(i,n) for(int i=0;i<n;++i)
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vii(a,n) vector<int>a(n);forn(i,n)cin>>a[i];
const int inf=1e18;
const int N=800;
string a[N];
int d[N][N];
int p[N][N];
int vis[N][N];
void clear(int n) {
forn(i,n) forn(j,n) d[i][j]=0;
}
void reset(int n) {
forn(i,n) forn(j,n) vis[i][j]=0;
}
struct z {
int f,s,d,d1,d2;
};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int n,s; cin>>n>>s;
forn(i,n) cin>>a[i];
forn(i,n) forn(j,n) p[i][j]=inf;
queue<z> q;
forn(i,n) forn(j,n) if (a[i][j]=='H') {
q.push({i,j,0});
}
while (q.size()) {
auto it=q.front(); q.pop();
int x=it.f, y=it.s, d=it.d;
if (x<0 || y<0) continue;
if (x>=n || y>=n) continue;
if (vis[x][y]) continue;
if (a[x][y]=='T' || a[x][y]=='D') continue;
vis[x][y]=1;
p[x][y]=d;
q.push({x-1,y,d+1});
q.push({x+1,y,d+1});
q.push({x,y-1,d+1});
q.push({x,y+1,d+1});
}
int l=-1, r=1e9;
pi D;
forn(i,n) forn(j,n) if (a[i][j]=='D') D={i,j};
while (l<r) {
int mid=(l+r+1)>>1;
reset(n);
forn(i,n) forn(j,n) if (a[i][j]=='M') {
q.push({i,j,0,0,0});
}
while (q.size()) {
auto it=q.front(); q.pop();
int x=it.f, y=it.s, d=it.d, d1=it.d1, d2=it.d2;
//cout<<mid<<": "<<x<<' '<<y<<' '<<d<<' '<<vis[x][y]<<' '<<p[x][y]<<' '<<a[x][y]<<'\n';
if (x<0 || y<0) continue;
if (x>=n || y>=n) continue;
if (vis[x][y]) continue;
if (a[x][y]=='T') continue;
if (p[x][y]<mid+(d+s-1)/s) continue;
if (p[x][y]==mid+(d+s-1)/s) {
if (d%s) {
} else {
continue;
}
}
vis[x][y]=1;
q.push({x-1,y,d+1});
q.push({x+1,y,d+1});
q.push({x,y-1,d+1});
q.push({x,y+1,d+1});
}
if (vis[D.f][D.s]) l=mid;
else r=mid-1;
}
cout<<r<<'\n';
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
Compilation message
mecho.cpp: In function 'void solve()':
mecho.cpp:72:32: warning: unused variable 'd1' [-Wunused-variable]
72 | int x=it.f, y=it.s, d=it.d, d1=it.d1, d2=it.d2;
| ^~
mecho.cpp:72:42: warning: unused variable 'd2' [-Wunused-variable]
72 | int x=it.f, y=it.s, d=it.d, d1=it.d1, d2=it.d2;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
297 ms |
13924 KB |
Output is correct |
8 |
Correct |
1 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
356 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
484 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
488 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
612 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
740 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
2 ms |
872 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
872 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
1008 KB |
Output is correct |
33 |
Correct |
15 ms |
4868 KB |
Output is correct |
34 |
Correct |
17 ms |
4948 KB |
Output is correct |
35 |
Correct |
65 ms |
5048 KB |
Output is correct |
36 |
Correct |
14 ms |
5680 KB |
Output is correct |
37 |
Correct |
21 ms |
5588 KB |
Output is correct |
38 |
Correct |
82 ms |
5768 KB |
Output is correct |
39 |
Correct |
19 ms |
6308 KB |
Output is correct |
40 |
Correct |
22 ms |
6368 KB |
Output is correct |
41 |
Correct |
92 ms |
6488 KB |
Output is correct |
42 |
Correct |
21 ms |
7124 KB |
Output is correct |
43 |
Correct |
18 ms |
7124 KB |
Output is correct |
44 |
Correct |
121 ms |
7228 KB |
Output is correct |
45 |
Correct |
24 ms |
7804 KB |
Output is correct |
46 |
Correct |
26 ms |
7852 KB |
Output is correct |
47 |
Correct |
164 ms |
7968 KB |
Output is correct |
48 |
Correct |
33 ms |
8552 KB |
Output is correct |
49 |
Correct |
30 ms |
8524 KB |
Output is correct |
50 |
Correct |
201 ms |
8724 KB |
Output is correct |
51 |
Correct |
36 ms |
9352 KB |
Output is correct |
52 |
Correct |
40 ms |
9300 KB |
Output is correct |
53 |
Correct |
249 ms |
9544 KB |
Output is correct |
54 |
Correct |
64 ms |
10108 KB |
Output is correct |
55 |
Correct |
84 ms |
10120 KB |
Output is correct |
56 |
Correct |
260 ms |
10260 KB |
Output is correct |
57 |
Correct |
46 ms |
10784 KB |
Output is correct |
58 |
Correct |
43 ms |
10788 KB |
Output is correct |
59 |
Correct |
317 ms |
11044 KB |
Output is correct |
60 |
Correct |
52 ms |
11660 KB |
Output is correct |
61 |
Correct |
54 ms |
11544 KB |
Output is correct |
62 |
Correct |
415 ms |
11836 KB |
Output is correct |
63 |
Correct |
214 ms |
11612 KB |
Output is correct |
64 |
Correct |
379 ms |
11732 KB |
Output is correct |
65 |
Correct |
385 ms |
11736 KB |
Output is correct |
66 |
Correct |
253 ms |
11728 KB |
Output is correct |
67 |
Correct |
231 ms |
11716 KB |
Output is correct |
68 |
Correct |
114 ms |
12260 KB |
Output is correct |
69 |
Correct |
109 ms |
12252 KB |
Output is correct |
70 |
Correct |
78 ms |
12284 KB |
Output is correct |
71 |
Correct |
113 ms |
12264 KB |
Output is correct |
72 |
Correct |
104 ms |
12272 KB |
Output is correct |
73 |
Correct |
120 ms |
17284 KB |
Output is correct |
74 |
Correct |
194 ms |
17292 KB |
Output is correct |
75 |
Correct |
225 ms |
17284 KB |
Output is correct |
76 |
Correct |
231 ms |
17288 KB |
Output is correct |
77 |
Correct |
195 ms |
17284 KB |
Output is correct |
78 |
Correct |
208 ms |
16592 KB |
Output is correct |
79 |
Correct |
182 ms |
16536 KB |
Output is correct |
80 |
Correct |
177 ms |
16648 KB |
Output is correct |
81 |
Correct |
210 ms |
16640 KB |
Output is correct |
82 |
Correct |
177 ms |
16564 KB |
Output is correct |
83 |
Correct |
219 ms |
15924 KB |
Output is correct |
84 |
Correct |
200 ms |
15784 KB |
Output is correct |
85 |
Correct |
226 ms |
15764 KB |
Output is correct |
86 |
Correct |
208 ms |
15988 KB |
Output is correct |
87 |
Correct |
200 ms |
15732 KB |
Output is correct |
88 |
Correct |
237 ms |
14924 KB |
Output is correct |
89 |
Correct |
248 ms |
14936 KB |
Output is correct |
90 |
Correct |
240 ms |
14924 KB |
Output is correct |
91 |
Correct |
219 ms |
14920 KB |
Output is correct |
92 |
Correct |
234 ms |
14948 KB |
Output is correct |