#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
char forest[805][805];
int n,s;
queue<pii> Q;
int timeBees[805][805];
int dx[4]={1,-1,0,0};
int dy[4]{0,0,1,-1};
void bfsBees(vector<pii> source){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
timeBees[i][j]=INT_MAX;
}
}
for(pii u: source){
timeBees[u.first][u.second]=0;
Q.push(u);
}
while(!Q.empty()){
pii u=Q.front();
Q.pop();
for(int mov=0;mov<4;mov++){
int i=u.first+dx[mov];
int j=u.second+dy[mov];
if(i<1 || i>n) continue;
if(j<1 || j>n) continue;
if(forest[i][j]=='T' || forest[i][j]=='D') continue;
if(timeBees[i][j]==INT_MAX){
timeBees[i][j]=timeBees[u.first][u.second]+1;
Q.push(pii(i,j));
}
}
}
}
pii start;
pii goal;
int dist[805][805];
int fuel[805][805];
bool valid(int moment){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=INT_MAX;
}
}
dist[start.first][start.second]=moment;
fuel[start.first][start.second]=0;
Q.push(start);
while(!Q.empty()){
pii u=Q.front();
Q.pop();
if(dist[u.first][u.second]==timeBees[u.first][u.second] && fuel[i][j]==0) continue;
for(int mov=0;mov<4;mov++){
int i=u.first+dx[mov];
int j=u.second+dy[mov];
if(i<1 || i>n) continue;
if(j<1 || j>n) continue;
if(forest[i][j]=='T' || forest[i][j]=='H') continue;
int nxt_dist=dist[u.first][u.second];
if(fuel[u.first][u.second]==0)
nxt_dist++;
if(timeBees[i][j]<nxt_dist) continue;
if(dist[i][j]==INT_MAX){
dist[i][j]=nxt_dist;
fuel[i][j]=(fuel[u.first][u.second]>0)? fuel[u.first][u.second]-1 : s-1;
Q.push(pii(i,j));
}
}
}
return dist[goal.first][goal.second]!=INT_MAX;
}
int binarySearch(){
int L=0,R=10000;
int mid=(L+R)/2;
int ans=-1;
while(L<=R){
mid=(L+R)/2;
if(valid(mid)){
ans=mid;
L=mid+1;
}
else{
R=mid-1;
}
}
return ans;
}
int main()
{
vector<pii> beeSource;
cin>>n>>s;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>forest[i][j];
if(forest[i][j]=='H'){
beeSource.push_back(pii(i,j));
}
else if(forest[i][j]=='M'){
start=pii(i,j);
}
else if(forest[i][j]=='D'){
goal=pii(i,j);
}
}
}
bfsBees(beeSource);
int last=binarySearch();
cout<<last<<'\n';
return 0;
}
Compilation message
mecho.cpp: In function 'bool valid(int)':
mecho.cpp:52:73: error: 'i' was not declared in this scope
52 | if(dist[u.first][u.second]==timeBees[u.first][u.second] && fuel[i][j]==0) continue;
| ^
mecho.cpp:52:76: error: 'j' was not declared in this scope
52 | if(dist[u.first][u.second]==timeBees[u.first][u.second] && fuel[i][j]==0) continue;
| ^