POJ 3626 BFS
思路:easy BFS
//By SiriusRen
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
queue<pair<int,int> >q;
int x,y,jyx,jyy,n,xx[]={1,-1,0,0},yy[]={0,0,1,-1},vis[1555][1555];
bool map[1555][1555];
bool check(int X,int Y){
return X>=0&&X<=1000&&Y>=0&&Y<=1000&&!map[X][Y];
}
int main(){
scanf("%d%d%d",&x,&y,&n);
x+=500,y+=500;
for(int i=1;i<=n;i++){
scanf("%d%d",&jyx,&jyy);
map[jyx+500][jyy+500]=1;
}
vis[500][500]=1;
q.push(make_pair(500,500));
while(!q.empty()){
int dx=q.front().first,dy=q.front().second;q.pop();
if(vis[x][y]){printf("%d\n",vis[x][y]-1);return 0;}
for(int i=0;i<4;i++){
int tx=dx+xx[i],ty=dy+yy[i];
if(!vis[tx][ty]&&check(tx,ty)){
vis[tx][ty]=vis[dx][dy]+1;
q.push(make_pair(tx,ty));
}
}
}
puts("-1");
}