刚改了下
```cpp
#include<iostream>
#include<queue>
using namespace std;
struct ill{
int h,l,step;
}s,a[2501];
queue<ill>q;
const int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int n,m,x,k;
bool f[51][51],b[2501];
bool all()
{
for(int i=1;i<=k;i++)
if(!b[i])
return 0;
return 1;
}
void find(ill x)
{
for(int i=1;i<=k;i++)
if(a[i].h==x.h&&a[i].l==x.l&&!b[i])
{
b[i]=1;
a[i].step=x.step;
return ;
}
}
void bfs()
{
ill s;
do
{
s=q.front();
q.pop();
for(int i=0;i<4;i++)
{
ill c;
c.h=s.h+dx[i],c.l=s.l+dy[i],c.step=s.step+1;
if(c.h>0&&c.h<=n&&c.l>0&&c.l<=m&&!f[c.h][c.l])
{
q.push(c);
f[c.h][c.l]=1;
find(c);
if(all())
{
for(int j=1;j<=k;j++)
cout<<a[j].step<<endl;
return ;
}
}
}
}while(!q.empty());
}
int main()
{
cin>>n>>m>>x>>k;
for(int i=1;i<=x;i++)
{
cin>>s.h>>s.l;
f[s.h][s.l]=1;
s.step=0;
q.push(s);
}
for(int i=1;i<=k;cin>>a[i].h>>a[i].l,i++);
bfs();
return 0;
}
```
by 开挂老司机 @ 2018-10-02 21:21:13