#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);
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});
}
}
}
f[ed.fi][ed.se] = oo;
//f(i,1,n){
// f(j,1,n) cout<<f[i][j]<<" ";
// cout<<endl;
//}
int l = 0, r = oo, mid, ans = -1;
while(l <= r){
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 |
3 ms |
2892 KB |
Output is correct |
2 |
Correct |
3 ms |
2892 KB |
Output is correct |
3 |
Correct |
3 ms |
2892 KB |
Output is correct |
4 |
Correct |
4 ms |
2892 KB |
Output is correct |
5 |
Correct |
3 ms |
2892 KB |
Output is correct |
6 |
Correct |
4 ms |
2892 KB |
Output is correct |
7 |
Correct |
67 ms |
6616 KB |
Output is correct |
8 |
Correct |
3 ms |
2884 KB |
Output is correct |
9 |
Correct |
3 ms |
2888 KB |
Output is correct |
10 |
Correct |
4 ms |
2884 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Incorrect |
2 ms |
3020 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
3020 KB |
Output isn't correct |
14 |
Correct |
5 ms |
3020 KB |
Output is correct |
15 |
Correct |
4 ms |
2892 KB |
Output is correct |
16 |
Correct |
4 ms |
2876 KB |
Output is correct |
17 |
Correct |
4 ms |
2880 KB |
Output is correct |
18 |
Correct |
3 ms |
2924 KB |
Output is correct |
19 |
Correct |
4 ms |
3020 KB |
Output is correct |
20 |
Correct |
3 ms |
2892 KB |
Output is correct |
21 |
Correct |
5 ms |
3020 KB |
Output is correct |
22 |
Correct |
3 ms |
3020 KB |
Output is correct |
23 |
Correct |
3 ms |
3012 KB |
Output is correct |
24 |
Correct |
4 ms |
3020 KB |
Output is correct |
25 |
Correct |
4 ms |
3020 KB |
Output is correct |
26 |
Correct |
4 ms |
3004 KB |
Output is correct |
27 |
Correct |
5 ms |
3148 KB |
Output is correct |
28 |
Correct |
4 ms |
3148 KB |
Output is correct |
29 |
Correct |
4 ms |
3148 KB |
Output is correct |
30 |
Correct |
3 ms |
3148 KB |
Output is correct |
31 |
Correct |
3 ms |
3144 KB |
Output is correct |
32 |
Correct |
4 ms |
3148 KB |
Output is correct |
33 |
Correct |
9 ms |
4300 KB |
Output is correct |
34 |
Correct |
9 ms |
4296 KB |
Output is correct |
35 |
Correct |
21 ms |
4392 KB |
Output is correct |
36 |
Correct |
12 ms |
4556 KB |
Output is correct |
37 |
Correct |
11 ms |
4556 KB |
Output is correct |
38 |
Correct |
25 ms |
4556 KB |
Output is correct |
39 |
Correct |
13 ms |
4816 KB |
Output is correct |
40 |
Correct |
13 ms |
4848 KB |
Output is correct |
41 |
Correct |
30 ms |
4860 KB |
Output is correct |
42 |
Correct |
14 ms |
5068 KB |
Output is correct |
43 |
Correct |
14 ms |
5068 KB |
Output is correct |
44 |
Correct |
37 ms |
5068 KB |
Output is correct |
45 |
Correct |
16 ms |
5320 KB |
Output is correct |
46 |
Correct |
18 ms |
5356 KB |
Output is correct |
47 |
Correct |
47 ms |
5344 KB |
Output is correct |
48 |
Correct |
21 ms |
5548 KB |
Output is correct |
49 |
Correct |
20 ms |
5572 KB |
Output is correct |
50 |
Correct |
52 ms |
5592 KB |
Output is correct |
51 |
Correct |
21 ms |
5840 KB |
Output is correct |
52 |
Correct |
21 ms |
5804 KB |
Output is correct |
53 |
Correct |
73 ms |
5920 KB |
Output is correct |
54 |
Correct |
24 ms |
6084 KB |
Output is correct |
55 |
Correct |
25 ms |
6044 KB |
Output is correct |
56 |
Correct |
71 ms |
6108 KB |
Output is correct |
57 |
Correct |
26 ms |
6344 KB |
Output is correct |
58 |
Correct |
28 ms |
6348 KB |
Output is correct |
59 |
Correct |
86 ms |
6356 KB |
Output is correct |
60 |
Correct |
38 ms |
6596 KB |
Output is correct |
61 |
Correct |
30 ms |
6528 KB |
Output is correct |
62 |
Correct |
89 ms |
6584 KB |
Output is correct |
63 |
Correct |
153 ms |
6504 KB |
Output is correct |
64 |
Correct |
203 ms |
6600 KB |
Output is correct |
65 |
Correct |
197 ms |
6592 KB |
Output is correct |
66 |
Correct |
165 ms |
6600 KB |
Output is correct |
67 |
Correct |
162 ms |
6592 KB |
Output is correct |
68 |
Correct |
121 ms |
6620 KB |
Output is correct |
69 |
Correct |
126 ms |
6724 KB |
Output is correct |
70 |
Correct |
111 ms |
6596 KB |
Output is correct |
71 |
Correct |
109 ms |
6608 KB |
Output is correct |
72 |
Incorrect |
110 ms |
6620 KB |
Output isn't correct |
73 |
Incorrect |
42 ms |
6852 KB |
Output isn't correct |
74 |
Correct |
60 ms |
6876 KB |
Output is correct |
75 |
Correct |
67 ms |
6864 KB |
Output is correct |
76 |
Correct |
56 ms |
6868 KB |
Output is correct |
77 |
Correct |
61 ms |
6884 KB |
Output is correct |
78 |
Correct |
59 ms |
6724 KB |
Output is correct |
79 |
Correct |
52 ms |
6844 KB |
Output is correct |
80 |
Correct |
54 ms |
6748 KB |
Output is correct |
81 |
Correct |
60 ms |
6852 KB |
Output is correct |
82 |
Correct |
53 ms |
6856 KB |
Output is correct |
83 |
Correct |
65 ms |
6744 KB |
Output is correct |
84 |
Correct |
60 ms |
6796 KB |
Output is correct |
85 |
Correct |
58 ms |
6720 KB |
Output is correct |
86 |
Correct |
64 ms |
6740 KB |
Output is correct |
87 |
Correct |
69 ms |
6808 KB |
Output is correct |
88 |
Correct |
67 ms |
6748 KB |
Output is correct |
89 |
Correct |
69 ms |
6752 KB |
Output is correct |
90 |
Correct |
68 ms |
6724 KB |
Output is correct |
91 |
Correct |
63 ms |
6736 KB |
Output is correct |
92 |
Correct |
65 ms |
6752 KB |
Output is correct |