https://www.luogu.org/problemnew/show/P1849

题目描述

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 

注意边界
 #include <cstring>
#include <cstdio>
#include <queue> inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
} const int N(1e3+); struct Node {int x,y;}u,v;
std::queue<Node>que; int cost[N][N];
bool link[N][N],inq[N][N]; int fx[]={,,,-};
int fy[]={,,-,}; inline int SPFA()
{
memset(cost,/,sizeof(cost));
que.push(u); cost[u.x][u.y]=;
for(; !que.empty(); )
{
u=que.front(); inq[u.x][u.y]=; que.pop();
for(int i=; i<; ++i)
{
v.x=u.x+fx[i],v.y=u.y+fy[i];
if(v.x<||v.y<||v.x>||v.y>) continue;
if(cost[v.x][v.y]>cost[u.x][u.y]+link[v.x][v.y])
{
cost[v.x][v.y]=cost[u.x][u.y]+link[v.x][v.y];
if(!inq[v.x][v.y]) inq[v.x][v.y]=,que.push(v);
}
}
}
return cost[][];
} int Presist()
{
int n; read(n);
read(u.x),read(u.y);
for(int x,y,i=; i<=n; ++i)
read(x),read(y),link[x][y]=;
printf("%d\n",SPFA());
return ;
} int Aptal=Presist();
int main(int argc,char**argv){;}

最新文章

  1. 尽快写完Math!
  2. javascript闭包学习例子
  3. Visual studio 2013安装
  4. CentOS/RHEL安装oracle 11G
  5. CDN技术发展趋势
  6. elipse插件整理
  7. Android 控件收集
  8. idl生成.h .c文件
  9. 一个自定义线程池的小Demo
  10. Apache的prefork模式和worker模式
  11. QML中多样化的ListModel(MultiDelegate)
  12. 剑指offer青蛙跳台阶问题
  13. # 20175333曹雅坤《Java程序设计》第1周学习总结
  14. Spring Boot 返回 JSON 数据,一分钟搞定!
  15. java基础知识—继承
  16. k8s网络
  17. 【hjmmm网络流24题补全计划】
  18. 6.25html基础!
  19. linux 系统运行级别一般为 0-6,请分别写出每个级别的含义
  20. appium+python 【Mac】Android夜神模拟器

热门文章

  1. destoon后台权限-不给客户创始人权限并屏蔽部分功能
  2. Linux下的硬件驱动——USB设备(转载)
  3. 如何用好 Google 等搜索引擎?
  4. java处理excel
  5. JAVA-基础(六) Java.io
  6. loj2280 「FJOI2017」矩阵填数
  7. luogu1262 间谍网络
  8. J​a​y​r​o​c​k​.​J​s​o​n​读​取​j​s​o​n​数​据​(​n​e​t​)
  9. Apache Log4j 2 is Coming
  10. [已解决]使用 apt-get update 命令提示 ...中被配置了多次