rt 样例过了
#include<bits/stdc++.h>
using namespace std;
struct cp{
int x, y;
};
int walk[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int a[20][20];
bool vis[20][20];
queue<cp> q;
int main()
{
memset(a, -1 ,sizeof(a));
memset(vis, false , sizeof(vis));
int n,m,t;
cin>>n>>m>>t;
int sx,sy,fx,fy;
cin>>sx>>sy>>fx>>fy;
for(int i = 1; i <= t; i++)
{
int tmp1,tmp2;
cin>>tmp1>>tmp2;
vis[tmp1][tmp2] = true;
}
cp tmp = {sx,sy};
vis[sx][sy] = true;
q.push(tmp);
while(!q.empty())
{
cp top = q.front();
int tx = top.x;
int ty = top.y;
q.pop();
for(int i = 0 ; i < 4; i++)
{
int nx = tx+walk[i][0];
int ny = ty+walk[i][1];
if(vis[nx][ny]||nx<1||nx>n||ny<1||ny>m)
{
continue;
}
int d = a[tx][ty];
a[nx][ny] = d+1;
cp in = {nx,ny};
q.push(in);
vis[nx][ny] = true;
}
}
cout<<a[fx][fy]<<endl;
return 0;
}