#include"bits/stdc++.h"
#define f(i,j,k) for(int i=j;i<=k;i++)
#define fi first
#define se second
#define mp make_pair
const int N = 808;
const int oo = 2e9;
const int C[] = {0,1,0,-1,0};
using namespace std;
using pii = pair<int,int>;
int n, s;
int f[N][N], cx[N][N];
char a[N][N];
pii st, ed;
struct dota{
int x, y, tim;
};
bool check(int t){
queue<dota> q;
memset(cx,0,sizeof(cx));
f(i,1,n) f(j,1,n) if(a[i][j] != 'T') cx[i][j] = 1;
q.push({st.fi,st.se,0});
cx[st.fi][st.se] = 0;
while(q.size()){
dota td = q.front(); q.pop();
int x = td.x;
int y = td.y;
int tim = td.tim;
//if(t == 1) cout<<x<<" "<<y<<" "<<tim<<endl;
if(x == ed.fi && y == ed.se) return true;
f(i,0,3){
int u = x + C[i];
int v = y + C[i+1];
if(cx[u][v] && (tim + 1)/s < f[u][v]-t){
q.push({u,v,tim+1});
cx[u][v] = 0;
}
}
}
return false;
}
main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>s;
f(i,1,n) f(j,1,n) cin>>a[i][j];
queue<pii> q;
memset(cx,0,sizeof(cx));
f(i,0,n) cx[i][0] = cx[0][i] = cx[i][n+1] = cx[n+1][i] = 1;
f(i,1,n) f(j,1,n){
if(a[i][j] == 'T') cx[i][j] = 1;
if(a[i][j] == 'H'){
cx[i][j] = 1;
q.push({i,j});
f[i][j] = 0;
}
if(a[i][j] == 'D') ed = mp(i,j), cx[i][j] = 1;
if(a[i][j] == 'M') st = mp(i,j);
}
while(q.size()){
pii td = q.front(); q.pop();
int x = td.fi, y = td.se;
f(i,0,3){
int u = x + C[i];
int v = y + C[i+1];
if(!cx[u][v]){
f[u][v] = f[x][y]+1;
cx[u][v] = true;
q.push({u,v});
}
}
}
//cout<<"vcl";
f[ed.fi][ed.se] = oo;
// f(i,1,n){
// f(j,1,n) cout<<f[i][j]<<" ";
// cout<<endl;
// }
int l = 0, r = f[st.fi][st.se]-1, mid, ans = -1;
while(l <= r){
//cout<<l<<" "<<r<<" "<<mid<<endl;
mid = (l+r)/2;
if(check(mid)) ans = mid,l = mid+1;
else r = mid-1;
}
cout<<ans;
}
Compilation message
mecho.cpp:47:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
47 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2892 KB |
Output is correct |
2 |
Correct |
2 ms |
2880 KB |
Output is correct |
3 |
Correct |
2 ms |
2884 KB |
Output is correct |
4 |
Correct |
1 ms |
2892 KB |
Output is correct |
5 |
Correct |
2 ms |
2880 KB |
Output is correct |
6 |
Correct |
2 ms |
2892 KB |
Output is correct |
7 |
Correct |
73 ms |
6544 KB |
Output is correct |
8 |
Correct |
2 ms |
2892 KB |
Output is correct |
9 |
Correct |
2 ms |
2892 KB |
Output is correct |
10 |
Correct |
2 ms |
2892 KB |
Output is correct |
11 |
Correct |
2 ms |
2892 KB |
Output is correct |
12 |
Correct |
2 ms |
3020 KB |
Output is correct |
13 |
Correct |
2 ms |
3020 KB |
Output is correct |
14 |
Correct |
3 ms |
3020 KB |
Output is correct |
15 |
Correct |
2 ms |
2892 KB |
Output is correct |
16 |
Correct |
3 ms |
2872 KB |
Output is correct |
17 |
Correct |
2 ms |
2888 KB |
Output is correct |
18 |
Correct |
2 ms |
2884 KB |
Output is correct |
19 |
Correct |
2 ms |
2884 KB |
Output is correct |
20 |
Correct |
2 ms |
2892 KB |
Output is correct |
21 |
Correct |
2 ms |
3008 KB |
Output is correct |
22 |
Correct |
2 ms |
3004 KB |
Output is correct |
23 |
Correct |
3 ms |
3020 KB |
Output is correct |
24 |
Correct |
2 ms |
3020 KB |
Output is correct |
25 |
Correct |
3 ms |
3020 KB |
Output is correct |
26 |
Correct |
3 ms |
3008 KB |
Output is correct |
27 |
Correct |
2 ms |
3136 KB |
Output is correct |
28 |
Correct |
2 ms |
3148 KB |
Output is correct |
29 |
Correct |
3 ms |
3148 KB |
Output is correct |
30 |
Correct |
2 ms |
3148 KB |
Output is correct |
31 |
Correct |
2 ms |
3148 KB |
Output is correct |
32 |
Correct |
2 ms |
3148 KB |
Output is correct |
33 |
Correct |
7 ms |
4268 KB |
Output is correct |
34 |
Correct |
7 ms |
4300 KB |
Output is correct |
35 |
Correct |
15 ms |
4376 KB |
Output is correct |
36 |
Correct |
8 ms |
4524 KB |
Output is correct |
37 |
Correct |
8 ms |
4524 KB |
Output is correct |
38 |
Correct |
25 ms |
4556 KB |
Output is correct |
39 |
Correct |
10 ms |
4828 KB |
Output is correct |
40 |
Correct |
14 ms |
4800 KB |
Output is correct |
41 |
Correct |
25 ms |
4868 KB |
Output is correct |
42 |
Correct |
13 ms |
5096 KB |
Output is correct |
43 |
Correct |
12 ms |
5068 KB |
Output is correct |
44 |
Correct |
32 ms |
5032 KB |
Output is correct |
45 |
Correct |
13 ms |
5220 KB |
Output is correct |
46 |
Correct |
13 ms |
5308 KB |
Output is correct |
47 |
Correct |
41 ms |
5248 KB |
Output is correct |
48 |
Correct |
15 ms |
5452 KB |
Output is correct |
49 |
Correct |
16 ms |
5516 KB |
Output is correct |
50 |
Correct |
58 ms |
5572 KB |
Output is correct |
51 |
Correct |
18 ms |
5800 KB |
Output is correct |
52 |
Correct |
16 ms |
5760 KB |
Output is correct |
53 |
Correct |
50 ms |
5816 KB |
Output is correct |
54 |
Correct |
20 ms |
5964 KB |
Output is correct |
55 |
Correct |
18 ms |
5984 KB |
Output is correct |
56 |
Correct |
58 ms |
6040 KB |
Output is correct |
57 |
Correct |
22 ms |
6216 KB |
Output is correct |
58 |
Correct |
22 ms |
6180 KB |
Output is correct |
59 |
Correct |
76 ms |
6256 KB |
Output is correct |
60 |
Correct |
26 ms |
6460 KB |
Output is correct |
61 |
Correct |
25 ms |
6352 KB |
Output is correct |
62 |
Correct |
79 ms |
6492 KB |
Output is correct |
63 |
Correct |
98 ms |
6472 KB |
Output is correct |
64 |
Correct |
175 ms |
6480 KB |
Output is correct |
65 |
Correct |
148 ms |
6472 KB |
Output is correct |
66 |
Correct |
119 ms |
6472 KB |
Output is correct |
67 |
Correct |
141 ms |
6480 KB |
Output is correct |
68 |
Correct |
73 ms |
6508 KB |
Output is correct |
69 |
Correct |
56 ms |
6500 KB |
Output is correct |
70 |
Correct |
50 ms |
6496 KB |
Output is correct |
71 |
Correct |
60 ms |
6496 KB |
Output is correct |
72 |
Correct |
61 ms |
6500 KB |
Output is correct |
73 |
Correct |
34 ms |
6732 KB |
Output is correct |
74 |
Correct |
48 ms |
6720 KB |
Output is correct |
75 |
Correct |
46 ms |
6744 KB |
Output is correct |
76 |
Correct |
50 ms |
6744 KB |
Output is correct |
77 |
Correct |
61 ms |
6732 KB |
Output is correct |
78 |
Correct |
53 ms |
6716 KB |
Output is correct |
79 |
Correct |
43 ms |
6596 KB |
Output is correct |
80 |
Correct |
47 ms |
6596 KB |
Output is correct |
81 |
Correct |
50 ms |
6592 KB |
Output is correct |
82 |
Correct |
51 ms |
6720 KB |
Output is correct |
83 |
Correct |
57 ms |
6676 KB |
Output is correct |
84 |
Correct |
51 ms |
6584 KB |
Output is correct |
85 |
Correct |
52 ms |
6632 KB |
Output is correct |
86 |
Correct |
62 ms |
6672 KB |
Output is correct |
87 |
Correct |
49 ms |
6584 KB |
Output is correct |
88 |
Correct |
56 ms |
6628 KB |
Output is correct |
89 |
Correct |
57 ms |
6592 KB |
Output is correct |
90 |
Correct |
62 ms |
6628 KB |
Output is correct |
91 |
Correct |
74 ms |
6544 KB |
Output is correct |
92 |
Correct |
68 ms |
6628 KB |
Output is correct |