#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
};
queue<node> q;
int i,j,sx,sy,fx,fy,t,step[1001][1001],vis[1001][1001],n,m;
int dx[5]={0,-1,1,0,0};
int dy[5]={0,0,0,1,-1};
void bfs()
{
node s;
s.x=sx,s.y=sy;
q.push(s);
vis[sx][sy]=1;
step[sx][sy]=1;
if(sx==fx&&sy==fy)
{
cout<<"1";
return;
}
while(!q.empty())
{
node a=q.front();
for(int i=1;i<=4;i++)
{
node now;
now.x=a.x+dx[i];
now.y=a.y+dy[i];
if(now.x<1||now.x>n||now.y<1||now.y>n||vis[now.x][now.y]==1)
continue;
step[now.x][now.y]=step[a.x][a.y]+1;
if(now.x==fx&&now.y==fy)
{
cout<<step[now.x][now.y];
return;
}
q.push(now);
}
q.pop();
}
cout<<"-1";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(vis,0,sizeof(vis));
cin>>n>>m>>t>>sx>>sy>>fx>>fy;
while(t--)
{
cin>>i>>j;
vis[i][j]=1;
}
bfs();
return 0;
}
有三个点MLE