题目描述

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相关推荐

  1. [洛谷P2698] [USACO12MAR]花盆Flowerpot

    洛谷题目链接:[USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and ...

  2. 洛谷P2698 [USACO12MAR]Flowerpot S

    P2698 [USACO12MAR]Flowerpot S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 思路: 宽度太小,第一滴和最后一滴的时间差只可能会小,宽度太大,时 ...

  3. 洛谷P2698 [USACO12MAR]花盆Flowerpot

    P2698 [USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and n ...

  4. 洛谷P3051 [USACO12MAR]Haybale Restacking

    [USACO12MAR]Haybale Restacking 源题目传送门  大意: 给出n块土地,现有泥土A[i],需要改造成B[i],但这次土地排列成环,且不可买进买出,只能运,且∑A[i]=∑B ...

  5. [luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)

    传送门 神奇的spfa #include <queue> #include <cstdio> #include <cstring> #include <ios ...

  6. #单调队列#洛谷 2698 [USACO12MAR]花盆Flowerpot

    题目 给出N滴水的坐标(X,Y),y表示水滴的高度,x表示它下落到x轴的位置.每滴水每秒从(x,y)到(x,y-1).你需要把花盆放在x轴上的某个位置,使得从开始接水到水滴完之间的时间差至少为D,只要 ...

  7. 洛谷 P2698 [USACO12MAR]花盆Flowerpot 单调队列

    https://www.luogu.org/problemnew/show/P2698 题意中文的不说了: 做法:就是一个滑动区间维护最大值和最小值,首先,了解一条性质,对于满足要求的两个区间 (l1 ...

  8. [洛谷P2698][USACO12MAR]花盆Flowerpot

    题目大意:$n$个坐标和时间.需要找到最小的一段区间使得这一段区间最大时间减去最小时间的差大于$d$ 题解:发现对于较优的区间$[l_i,r_i]$(即对于这个左端点,$r_i$是第一个符合条件的), ...

  9. 洛谷 P2698 [USACO12MAR]花盆Flowerpot(抄)

    题目描述 Farmer John has been having trouble making his plants grow, and needs your help to water them p ...

最新文章

  1. 第一个spring程序
  2. 下拉推广系统立择火星推荐_下拉词优化不仅仅优化百度,其实还可以优化抖音、京东和阿里巴巴...
  3. 读写锁的由奢入俭“易”
  4. ligerui layout拖动中间分隔条宽度后,重新调整两边控件的宽度
  5. leetcode 1047. Remove All Adjacent Duplicates In String | 1047. 删除字符串中的所有相邻重复项(Java)
  6. mysql zip 安装
  7. mysql数据库工程师网易微专业_网易MySQL数据库工程师微专业学习笔记(五)
  8. 一个待办事列表todolist
  9. jmeter性能测试报告_用 JMeter 做接口测试的优劣浅析
  10. pandas分析各国家交易情况
  11. android点击通知栏之后消失,为什么noftifications出现在android通知栏中一段时间​​然后消失...
  12. 心情随笔(二):坚持就是胜利!
  13. 基于边缘的主动轮廓模型——从零到一用python实现snake
  14. 周立功CAN通讯(txt格式) 报文解析
  15. 影评分析第3篇 上映4天,票房7.4亿的《海王》,用数据看大片!
  16. 一个屌丝程序猿的人生(四十二)
  17. 最简单明了的QT服务器搭建
  18. JAVA基础算法练习(5):行星碰撞
  19. 基于 Amazon Amplify 构建自己的首个 iOS 应用程序(二)
  20. Origin更改图的尺寸大小

热门文章

  1. 81.游戏项目-物体任意角度飞行和停止
  2. 数据仓库/集市 星形/雪花形 事实/维度表
  3. 小康陪你学JAVA--------三大循环之Do-while循环
  4. 今天修改了数据库结构,XSD文件都要重新生成,郁闷!
  5. Ubuntu/Fedora高版本安装海思SDK的方法
  6. 中国3大移动公司(电信,联通,移动)频率分配大全(GSM,CDMA,CDMA2000,WCDMA,TD-SCDMA,LTE TD,FDD)
  7. 802.11发展到802.11g历程
  8. 嵌入式linux ext4映像制作工具说明 real210_v62
  9. java 区间树_线段树(区间树)之区间染色和4n推导过程
  10. LVS+Keepalive 实现负载均衡高可用集群