WA 5
Here's my code-
#include <bits/stdc++.h>
using namespace std;
bool upp,downn,leftt,rightt,avleftt,avrightt,avupp,avdownn;
bool vis[105][105],vis2[105][105];
int dist[105][105];
int dist2[105][105];
int xmov[4]={0,-1,0,1};
int ymov[4]={-1,0,1,0};
int main()
{
// cout << "Hello World!" << endl;
ios::sync_with_stdio(false);
for(int i=1;i<105;i++)for(int j=1;j<105;j++)dist[i][j]=INT_MAX;
int n,m,l,x1,y1,x2,y2;
cin>>n>>m>>l;
cin>>x1>>y1;
cin>>x2>>y2;
queue <pair <int,int> > q;
queue <int> distt;
int ans=0;
if(abs(x1-x2)+abs(y1-y2)==1){
ans=1;
}
vis[x1][y1]=true;
dist[x1][y1]=0;
vis[x2][y2]=true;
q.push(make_pair(x1,y1));
distt.push(0);
while(q.empty()==0){
pair <int,int> topp=q.front();
int curdist=distt.front();
q.pop();
distt.pop();
int curx,cury,nextx,nexty;
curx=topp.first;
cury=topp.second;
for(int i=0;i<4;i++){
nextx=curx+xmov[i];
nexty=cury+ymov[i];
if(nextx>=1&&nextx<=n&&nexty>=1&&nexty<=m){
if(vis[nextx][nexty]==false){
vis[nextx][nexty]=true;
q.push(make_pair(nextx,nexty));
distt.push(curdist+1);
dist[nextx][nexty]=curdist+1;
}
}
}
}
q.push(make_pair(x2,y2));
distt.push(-1);
vis2[x2][y2]=true;
dist2[x2][y1]=-1;
while(q.empty()==0){
pair <int,int> topp=q.front();
int curdist=distt.front();
q.pop();
distt.pop();
int curx,cury,nextx,nexty;
curx=topp.first;
cury=topp.second;
for(int i=0;i<4;i++){
nextx=curx+xmov[i];
nexty=cury+ymov[i];
if(nextx>=1&&nextx<=n&&nexty>=1&&nexty<=m){
if(vis2[nextx][nexty]==false){
vis2[nextx][nexty]=true;
q.push(make_pair(nextx,nexty));
distt.push(curdist+1);
dist2[nextx][nexty]=curdist+1;
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i==x2&&j==y2){
continue;
}
if(dist[i][j]!=INT_MAX){
cout<<i<<" ::::: "<<j<<"\n";
int totaldist=dist[i][j]+dist2[i][j];
if(totaldist<=l){
ans=max(ans,max(abs(y2-j),abs(x2-i))+1);
}
}
}
}
cout<<ans;
return 0;
}