将点权为1的插入对尾,为0的插入队头
#include<iostream>
#include<algorithm>
#include<cstring>
#include<deque>
using namespace std;
const int N = 2010;
typedef pair<int,int> p;
int dist[N][N];
int d[N][N];
int n,x0,y0;
void bfs(){
memset(dist,0x3f,sizeof dist);
deque<p> q;
q.push_back({x0,y0});
dist[x0][y0] = 0;
int dx[4] = {0,1,0,-1},dy[4] = {-1,0,1,0};
while(q.size()){
p t = q.front();
q.pop_front();
for(int i = 0;i < 4;i ++){
int x = t.first + dx[i],y = t.second + dy[i];
if(x < 0 || y < 0 || x > 1003 || y > 1003) continue;
if(dist[x][y] > dist[t.first][t.second] + d[x][y]){
dist[x][y] = dist[t.first][t.second] + d[x][y];
if(d[x][y]) q.push_back({x,y});
else q.push_front({x,y});
}
}
}
}
int main(){
cin >> n >> x0 >> y0;
for(int i = 0;i < n;i ++){
int a,b;
cin >> a >> b;
d[a][b] = 1;
}
bfs();
cout << dist[0][0];
}