题目描述

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<cstdio>
#include<cstring>
const int maxn=1e3+;
int N,x,y,a,b,c,d;
int s[maxn][maxn];
bool map[maxn][maxn];
int q[maxn*maxn][],head,tail;
int hb[]={,-,,};
int lb[]={,,,-};
void SPFA(){
s[x][y]=,q[tail][]=x,q[tail++][]=y;
while(head!=tail){
a=q[head][],b=q[head++][],head%=maxn*maxn;
for(int i=;i<;i++){
c=a+hb[i],d=b+lb[i];
if(c>=&&c<=&&d>=&&d<=)
if(s[c][d]>s[a][b]+map[c][d]){
s[c][d]=s[a][b]+map[c][d];
q[tail][]=c,q[tail++][]=d,tail%=maxn*maxn;
}
}
}
}
int main(){
scanf("%d%d%d",&N,&x,&y);
for(int i=;i<=N;i++) scanf("%d%d",&a,&b),map[a][b]=;
memset(s,,sizeof(s));SPFA();
printf("%d\n",s[][]);
return ;
}

[USACO12MAR]拖拉机的更多相关文章

  1. 洛谷 P1849 [USACO12MAR]拖拉机Tractor

    题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...

  2. 洛谷—— P1849 [USACO12MAR]拖拉机Tractor

    https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...

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

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

  4. NowCoderWannafly挑战赛5-可编程拖拉机比赛-向上取整和向下取整函数

    可编程拖拉机比赛 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 "这个比赛,归根结底 ...

  5. 洛谷P2698 [USACO12MAR]花盆Flowerpot

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

  6. P2698 [USACO12MAR]花盆Flowerpot(单调队列+二分)

    P2698 [USACO12MAR]花盆Flowerpot 一看标签........十分后悔 标签告诉你单调队列+二分了............ 每次二分花盆长度,蓝后开2个单调队列维护最大最小值 蓝 ...

  7. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  8. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  9. 牛客网 Wannafly挑战赛5 B.可编程拖拉机比赛-ceil()函数+floor()函数

    可编程拖拉机比赛 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 “这个比赛,归根结底就是控制一 ...

随机推荐

  1. spring Existing transaction found for transaction marked with propagation 'never' 解决

    先在申明事务中配置了所有的事务 <!--配置事物传播策略,以及隔离级别--> <tx:advice id="txAdvice" transaction-manag ...

  2. java中properties的使用实例

    package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputSt ...

  3. upupw nginx服务器 rewrite设置

    最近开始尝试使用upupw的Nginx套件做开发,感觉还挺不错的,也遇到了一些问题,决定在这里记录一下,同时也希望可以帮助到一些人. 用习惯了Apache,改用Nginx之后会有些不适应,但是咬咬牙就 ...

  4. Windows Server 启用匿名共享

    1.开始 → 运行 → gpedit.msc,打开组策略编辑器: 2.依次展开"计算机配置" → "windows设置" → "安全设置"  ...

  5. (转)Spring简介

    http://blog.csdn.net/yerenyuan_pku/article/details/52830571 阿昀以一个初学者的身份学习Spring框架,并按照一个小白的思路详细总结学习过程 ...

  6. java工作流activiti的步骤

    链接:activiti 表名称的解释 工作流从流程定义到创建一个流程实例完成执行步骤(省略bpmn的画法) 工作流的所有操作都是使用流程引擎来进行操作的,流程引擎只是存储流程的过程,而不存储具体的业务 ...

  7. CodeForces - 1059D——二分/三分

    题目 题目链接 简单的说,就是作一个圆包含所有的点且与x轴相切,求圆的最小半径 方法一 分析:求最小,对半径而言肯定满足单调性,很容易想到二分.我们二分半径,然后由于固定了与X轴相切,我们对于每一个点 ...

  8. python基础一 day5 知识点

    Unicode转化为gbk和utf-8 表现形式:str转化为bytes

  9. CWnd::Updata的作用

    CWnd::Updata的作用 CWnd::UpdateData 调用此成员函数以在对话框中初始化数据,或者取回和验证对话框数据. BOOL UpdateData(BOOL bSaveAndValid ...

  10. 数位DP || Gym 101653R Ramp Number

    每一位都大于等于前一位的数叫Ramp Number 给一个数,如果不是Ramp Number输出-1,如果是Ramp Number输出比它小的Ramp Number的个数 只和每一位上的数字有关 #i ...