#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
#define ff first
#define ss second
const ll mod = 1000000007;
void solve(){
ll n,s; cin>>n>>s;
char c[n+1][n+1];
vector<pl> hives;
ll dis[n+1][n+1]; forp(i,1,n){forp(j,1,n){dis[i][j]=INT_MAX;}}
pl start,end;
forp(i,1,n){
forp(j,1,n){
cin>>c[i][j];
if(c[i][j]=='H'){
hives.pb({i,j});
dis[i][j]=0;
}
if(c[i][j]=='T'){
dis[i][j]=0;
}
if(c[i][j]=='M'){
start.ff=i; start.ss=j;
}
if(c[i][j]=='D'){
end.ff=i; end.ss=j;
}
}
}
bool vis[n+2][n+2];
queue<pl> q;
ll i,j;
for(auto p:hives){
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
q.push(p);
vis[p.ff][p.ss]=1;
while(!q.empty()){
i=q.front().ff; j=q.front().ss;
q.pop();
if(!vis[i][j+1] && (c[i][j+1]!='T' && c[i][j+1]!='H')){
vis[i][j+1]=1;
dis[i][j+1]=min(dis[i][j+1],dis[i][j]+1);
q.push({i,j+1});
}
if(!vis[i+1][j] && (c[i+1][j]!='T' && c[i+1][j]!='H')){
vis[i+1][j]=1;
dis[i+1][j]=min(dis[i+1][j],dis[i][j]+1);
q.push({i+1,j});
}
if(!vis[i][j-1] && (c[i][j-1]!='T' && c[i][j-1]!='H')){
vis[i][j-1]=1;
dis[i][j-1]=min(dis[i][j-1],dis[i][j]+1);
q.push({i,j-1});
}
if(!vis[i-1][j] && (c[i-1][j]!='T' && c[i-1][j]!='H')){
vis[i-1][j]=1;
dis[i-1][j]=min(dis[i-1][j],dis[i][j]+1);
q.push({i-1,j});
}
}
}
ll l=0,r=n,mid,len;
queue<pair<pl,ll>> qq;
while(l<r){
mid=(l+r+1)/2;
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
qq.push({start,0});
vis[start.ff][start.ss]=1;
while(!qq.empty()){
i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
qq.pop();
if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
vis[i][j+1]=1;
qq.push({{i,j+1},len+1});
}
if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
vis[i+1][j]=1;
qq.push({{i+1,j},len+1});
}
if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
vis[i][j-1]=1;
qq.push({{i,j-1},len+1});
}
if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
vis[i-1][j]=1;
qq.push({{i-1,j},len+1});
}
}
if(vis[end.ff][end.ss]){
l=mid;
} else {
r=mid-1;
}
}
mid=0;
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
qq.push({start,0});
vis[start.ff][start.ss]=1;
while(!qq.empty()){
i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
qq.pop();
if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
vis[i][j+1]=1;
qq.push({{i,j+1},len+1});
}
if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
vis[i+1][j]=1;
qq.push({{i+1,j},len+1});
}
if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
vis[i][j-1]=1;
qq.push({{i,j-1},len+1});
}
if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
vis[i-1][j]=1;
qq.push({{i-1,j},len+1});
}
}
if(vis[end.ff][end.ss]){
cout<<l<<newl;
} else {
cout<<-1<<newl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output3.txt","w",stdout);
#endif
int t=1; //cin>>t;
while(t--)solve();
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | freopen("output3.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
92 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
3 |
Runtime error |
86 ms |
65540 KB |
Execution killed with signal 9 |
4 |
Runtime error |
87 ms |
65540 KB |
Execution killed with signal 9 |
5 |
Runtime error |
89 ms |
65540 KB |
Execution killed with signal 9 |
6 |
Runtime error |
91 ms |
65540 KB |
Execution killed with signal 9 |
7 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
9 |
Runtime error |
86 ms |
65536 KB |
Execution killed with signal 9 |
10 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
368 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
14 |
Runtime error |
82 ms |
65540 KB |
Execution killed with signal 9 |
15 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
18 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
19 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
21 |
Runtime error |
87 ms |
65540 KB |
Execution killed with signal 9 |
22 |
Runtime error |
91 ms |
65540 KB |
Execution killed with signal 9 |
23 |
Incorrect |
3 ms |
332 KB |
Output isn't correct |
24 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
26 |
Runtime error |
87 ms |
65540 KB |
Execution killed with signal 9 |
27 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
28 |
Runtime error |
87 ms |
65540 KB |
Execution killed with signal 9 |
29 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
30 |
Runtime error |
86 ms |
65540 KB |
Execution killed with signal 9 |
31 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
33 |
Runtime error |
98 ms |
65540 KB |
Execution killed with signal 9 |
34 |
Runtime error |
93 ms |
65540 KB |
Execution killed with signal 9 |
35 |
Runtime error |
86 ms |
65540 KB |
Execution killed with signal 9 |
36 |
Runtime error |
84 ms |
65540 KB |
Execution killed with signal 9 |
37 |
Runtime error |
99 ms |
65540 KB |
Execution killed with signal 9 |
38 |
Runtime error |
88 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Runtime error |
88 ms |
65540 KB |
Execution killed with signal 9 |
40 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
41 |
Runtime error |
85 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
43 |
Runtime error |
89 ms |
65540 KB |
Execution killed with signal 9 |
44 |
Runtime error |
93 ms |
65540 KB |
Execution killed with signal 9 |
45 |
Runtime error |
92 ms |
65540 KB |
Execution killed with signal 9 |
46 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
47 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
48 |
Runtime error |
90 ms |
65540 KB |
Execution killed with signal 9 |
49 |
Runtime error |
89 ms |
65536 KB |
Execution killed with signal 9 |
50 |
Incorrect |
3 ms |
332 KB |
Output isn't correct |
51 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
52 |
Runtime error |
93 ms |
65536 KB |
Execution killed with signal 9 |
53 |
Runtime error |
87 ms |
65540 KB |
Execution killed with signal 9 |
54 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
55 |
Runtime error |
100 ms |
65540 KB |
Execution killed with signal 9 |
56 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
57 |
Runtime error |
89 ms |
65540 KB |
Execution killed with signal 9 |
58 |
Incorrect |
3 ms |
332 KB |
Output isn't correct |
59 |
Runtime error |
94 ms |
65540 KB |
Execution killed with signal 9 |
60 |
Runtime error |
89 ms |
65540 KB |
Execution killed with signal 9 |
61 |
Runtime error |
88 ms |
65540 KB |
Execution killed with signal 9 |
62 |
Runtime error |
93 ms |
65540 KB |
Execution killed with signal 9 |
63 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
64 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
65 |
Incorrect |
3 ms |
332 KB |
Output isn't correct |
66 |
Runtime error |
88 ms |
65540 KB |
Execution killed with signal 9 |
67 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
68 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
69 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
70 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
71 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
72 |
Runtime error |
95 ms |
65540 KB |
Execution killed with signal 9 |
73 |
Runtime error |
87 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
93 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
90 ms |
65540 KB |
Execution killed with signal 9 |
76 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
77 |
Runtime error |
90 ms |
65540 KB |
Execution killed with signal 9 |
78 |
Runtime error |
96 ms |
65540 KB |
Execution killed with signal 9 |
79 |
Runtime error |
93 ms |
65540 KB |
Execution killed with signal 9 |
80 |
Runtime error |
86 ms |
65540 KB |
Execution killed with signal 9 |
81 |
Runtime error |
90 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
91 ms |
65540 KB |
Execution killed with signal 9 |
83 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
84 |
Runtime error |
96 ms |
65540 KB |
Execution killed with signal 9 |
85 |
Runtime error |
90 ms |
65540 KB |
Execution killed with signal 9 |
86 |
Runtime error |
86 ms |
65540 KB |
Execution killed with signal 9 |
87 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
88 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
89 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
90 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
91 |
Runtime error |
93 ms |
65540 KB |
Execution killed with signal 9 |
92 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |