#include <bits/stdc++.h>
using namespace std;
const long long MAX=810;
long long n,s;
char mat[MAX][MAX];
long long si,sj,ei,ej;
long long di[4]= {0,0,-1,1};
long long dj[4]= {1,-1,0,0};
vector<pair<long long,long long> > hives;
long long distmecho[MAX][MAX];
long long occupied[MAX][MAX];
bool canspreadhive(long long i, long long j)
{
if(i<0 || i>=n || j<0 || j>=n || occupied[i][j]!=-1 || mat[i][j]=='T' || mat[i][j]=='D')return false;
return true;
}
void spread_hives()
{
memset(occupied,-1,sizeof(occupied));
queue<pair<long long,long long> > q;
for(auto x:hives)
{
q.push(x);
occupied[x.first][x.second]=0;
}
while(!q.empty())
{
auto curr=q.front();
q.pop();
for(long long k=0; k<4; k++)
{
long long newi=curr.first+di[k];
long long newj=curr.second+dj[k];
if(!canspreadhive(newi,newj))continue;
occupied[newi][newj]=occupied[curr.first][curr.second]+1;
q.push({newi,newj});
}
}
}
bool canmechostep(long long i, long long j)
{
if(i<0 || i>=n || j<0 || j>=n || distmecho[i][j]!=-1 || mat[i][j]=='H' || mat[i][j]=='T')return false;
return true;
}
bool can_stay(long long X)
{
memset(distmecho,-1,sizeof(distmecho));
distmecho[si][sj]=0;
queue<pair<long long,long long> > q;
q.push({si,sj});
if(occupied[si][sj]<=X)return false;
while(!q.empty())
{
auto curr=q.front();
q.pop();
for(long long k=0; k<4; k++)
{
long long newi=curr.first+di[k];
long long newj=curr.second+dj[k];
if(!canmechostep(newi,newj))continue;
long long newtime=distmecho[curr.first][curr.second]+1;
long long tmp=newtime;
long long use=tmp/s+X;
if(tmp%s)use++;
if(newi==ei && newj==ej)return true;
if(use<=occupied[newi][newj]))
{
distmecho[newi][newj]=newtime;
q.push({newi,newj});
}
}
}
if(distmecho[ei][ej]!=-1)return true;
else return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>s;
for(long long i=0; i<n; i++)
{
for(long long j=0; j<n; j++)
{
cin>>mat[i][j];
if(mat[i][j]=='M')
{
si=i;
sj=j;
}
else if(mat[i][j]=='D')
{
ei=i;
ej=j;
}
else if(mat[i][j]=='H')
{
hives.push_back({i,j});
}
}
}
spread_hives();
long long B=0,E=1e6;
long long res=-1;
while(B<=E)
{
long long mid=(B+E)/2;
if(can_stay(mid))
{
res=mid;
B=mid+1;
}
else
{
E=mid-1;
}
}
cout<<res<<endl;
return 0;
}
Compilation message
mecho.cpp: In function 'bool can_stay(long long int)':
mecho.cpp:67:42: error: expected primary-expression before ')' token
67 | if(use<=occupied[newi][newj]))
| ^