题目描述

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. AJPFX总结方法的特点

    它可以实现独立的功能; 必须定义在类里面; 它只有被调用才会执行; 它可以被重复使用; 方法结束后方法里的对象失去引用;   如何定义一个功能,并通过方法体现出来: ① 明确该功能运算后的结果.明确返 ...

  2. poj1862 Stripies

    思路: 简单贪心. 实现: #include <iostream> #include <cstdio> #include <algorithm> #include ...

  3. CCF|中间数|Java

    import java.util.*; public class tyt { public static void main(String[] args) { Scanner in = new Sca ...

  4. 程序员必须知道FTP命令

                                             程序员必须知道FTP命令 文件传输软件的使用格式为:FTP<FTP地址>,若连 接成功,系统将提示用户输入 ...

  5. 几个windows使用小技巧

    windows使用技巧 保存网页上图片时,可以按住左键把图片拖到右下角(win+D,双屏幕直接拖动)然后就可以放在桌面啦 放大镜-->Win+加号或者减号(放大或缩小).Win + Esc(退出 ...

  6. 学习Python的一些Tips

    0. Python安装 官网提供多种方式,一般Windows下直接安装exe即可:Linux下基本上自带python:另外也提供源码,也可自行编译: 若安装后无法使用,则检查一下环境变量是否设置正确. ...

  7. python3 进程与线程

    1.进程定义 狭义的定义: 进程是正在运行的程序的实例. 广义的定义:进程是一个具有 一定独立功能的程序关于某个数据集合的一次运行活动.它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体.它 ...

  8. zabbix利用微信报警

    一.背景介绍 报警的方式有很多种,比如常用的邮件报警,短信,微信等.那这篇文章主要是实现微信报警. 值得注意的是,之前使用微信企业公众号,现在微信企业公众号更新成企业微信了.所以直接注册企业微信,进入 ...

  9. vueshengmingzhouqi

    首先,每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期.首先看一张图吧~这是官方文档上的图片相信大家一定都会很熟悉: 可以看到在vue一整个的生命周期中会有很多钩子函 ...

  10. luogu 1968 美元汇率

    https://www.luogu.org/problemnew/show/P1968 定义二维数组f[ ][ ],f[i][1]表示在第i天将马克变为美元,f[i][2]表示在第i天将美元变为马克. ...