After a long day of work, Farmer John completely forgot that he left his tractor in the middle of the field. His cows, always up to no good, decide to play a prank of Farmer John: they deposit N bales of hay (1 <= N <= 50,000) at various locations in the field, so that Farmer John cannot easily remove the tractor without first removing some of the bales of hay.ide
The location of the tractor, as well as the locations of the N hay bales, are all points in the 2D plane with integer coordinates in the range 1..1000. There is no hay bale located at the initial position of the tractor. When Farmer John drives his tractor, he can only move it in directions that are parallel to the coordinate axes (north, south, east, and west), and it must move in a sequence of integer amounts. For example, he might move north by 2 units, then east by 3 units. The tractor cannot move onto a point occupied by a hay bale.spa
Please help Farmer John determine the minimum number of hay bales he needs to remove so that he can free his tractor (that is, so he can drive his tractor to the origin of the 2D plane).code
通過一天漫長的工做,農場主 John 徹底忘記了他的拖拉機還在場地中央。他的奶牛們總喜歡和他搞些惡做劇,它們在場地的不一樣位置丟下 N(1 ≤ N ≤ 50,000)堆乾草。這樣 John 就必須先移走一些乾草堆才能將拖拉機開走。blog
拖拉機和乾草堆均可以看做是二維平面上的點,它們的座標都是整數,座標範圍在 1 到1000 之間。沒有那堆乾草的座標和拖拉機的初始座標一致。John 駕駛拖拉機只能沿着座標軸的方向移動若干單位長度,好比說,他能夠先朝北移動 2 個單位長度,再向東移動 3 個單位長度等等。拖拉機不能移動到乾草堆所佔據的點。ci
請你幫助 John 計算一下,最少要移動多少堆乾草才能將拖拉機開會座標原點。rem
第一行,三個用空格隔開的整數 N、x、y,表示有N 堆乾草和拖拉機的起始座標。string
第 2行到第N+1 行,每行兩個用空格隔開的整數 x、y,表示每堆乾草的座標。it
輸出格式:一行一個整數,表示最少要移動多少堆乾草 John 才能將拖拉機開會座標原點。io
若是這也叫SPFA的話;ast
1 #include<cstdio> 2 #include<cstring> 3 const int maxn=1e3+10; 4 int N,x,y,a,b,c,d; 5 int s[maxn][maxn]; 6 bool map[maxn][maxn]; 7 int q[maxn*maxn][2],head,tail; 8 int hb[4]={1,-1,0,0}; 9 int lb[4]={0,0,1,-1}; 10 void SPFA(){ 11 s[x][y]=0,q[tail][0]=x,q[tail++][1]=y; 12 while(head!=tail){ 13 a=q[head][0],b=q[head++][1],head%=maxn*maxn; 14 for(int i=0;i<4;i++){ 15 c=a+hb[i],d=b+lb[i]; 16 if(c>=0&&c<=1001&&d>=0&&d<=1001) 17 if(s[c][d]>s[a][b]+map[c][d]){ 18 s[c][d]=s[a][b]+map[c][d]; 19 q[tail][0]=c,q[tail++][1]=d,tail%=maxn*maxn; 20 } 21 } 22 } 23 } 24 int main(){ 25 scanf("%d%d%d",&N,&x,&y); 26 for(int i=1;i<=N;i++) scanf("%d%d",&a,&b),map[a][b]=1; 27 memset(s,30,sizeof(s));SPFA(); 28 printf("%d\n",s[0][0]); 29 return 0; 30 }