洛谷 P1849 [USACO12MAR]拖拉机Tractor
題目描述
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.
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.
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).
經過一天漫長的工作,農場主 John 完全忘記了他的拖拉機還在場地中央。他的奶牛們總喜歡和他搞些惡作劇,它們在場地的不同位置丟下 N(1 ≤ N ≤ 50,000)堆干草。這樣 John 就必須先移走一些干草堆才能將拖拉機開走。
拖拉機和干草堆都可以看作是二維平面上的點,它們的坐標都是整數,坐標范圍在 1 到1000 之間。沒有那堆干草的坐標和拖拉機的初始坐標一致。John 駕駛拖拉機只能沿著坐標軸的方向移動若干單位長度,比如說,他可以先朝北移動 2 個單位長度,再向東移動 3 個單位長度等等。拖拉機不能移動到干草堆所占據的點。
請你幫助 John 計算一下,最少要移動多少堆干草才能將拖拉機開會坐標原點。
輸入輸出格式
輸入格式:
?
第一行,三個用空格隔開的整數 N、x、y,表示有N 堆干草和拖拉機的起始坐標。
第 2行到第N+1 行,每行兩個用空格隔開的整數 x、y,表示每堆干草的坐標。
?
輸出格式:
?
一行一個整數,表示最少要移動多少堆干草 John 才能將拖拉機開會坐標原點。
輸入輸出樣例
輸入樣例#1:?7 6 3 6 2 5 2 4 3 2 1 7 3 5 4 6 4 輸出樣例#1:?
1
spfa
#include <cstring> #include <cstdio> #include <queue> #define N 1005 using namespace std; queue<pair<int,int> >q; bool vis[N][N]; int n,x,y,fx[5]={1,-1,0,0},fy[5]={0,0,-1,1},DIS[N][N],gc[N][N]; void spfa() {q.push(make_pair(x,y)); for(int nx,ny;!q.empty();){nx=q.front().first,ny=q.front().second;q.pop();vis[nx][ny]=false;for(int i=0;i<4;++i){int tx=nx+fx[i],ty=ny+fy[i];if(tx>=0&&tx<=1001&&ty>=0&&ty<=1001&&DIS[tx][ty]>DIS[nx][ny]+gc[tx][ty]){DIS[tx][ty]=DIS[nx][ny]+gc[tx][ty];if(!vis[tx][ty]){vis[tx][ty]=true;q.push(make_pair(tx,ty));}}}} } int main(int argc,char *argv[]) {scanf("%d%d%d",&n,&x,&y);for(int a,b,i=1;i<=n;++i){scanf("%d%d",&a,&b);gc[a][b]=1;}memset(DIS,0x3f,sizeof(DIS));DIS[x][y]=0;spfa();printf("%d\n",DIS[0][0]);return 0; }?
轉載于:https://www.cnblogs.com/ruojisun/p/7742176.html
總結
以上是生活随笔為你收集整理的洛谷 P1849 [USACO12MAR]拖拉机Tractor的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 04-String——课后作业1:字串加
- 下一篇: mysql递归