题目描述

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[]={,-,,},fy[]={,,-,},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=;i<;++i)
{
int tx=nx+fx[i],ty=ny+fy[i];
if(tx>=&&tx<=&&ty>=&&ty<=&&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=;i<=n;++i)
{
scanf("%d%d",&a,&b);
gc[a][b]=;
}
memset(DIS,0x3f,sizeof(DIS));
DIS[x][y]=;
spfa();
printf("%d\n",DIS[][]);
return ;
}

洛谷 P1849 [USACO12MAR]拖拉机Tractor的更多相关文章

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

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

  2. 洛谷P2698 [USACO12MAR]花盆Flowerpot

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

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

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

  4. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]

    题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...

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

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

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

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

  7. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  8. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  9. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

随机推荐

  1. POJ 2441 Arrange the Bulls(状态压缩DP)

    题意很简单,n头牛,m个位置,每头牛有各自喜欢的位置,问安排这n头牛使得每头牛都在各自喜欢的位置有几种安排方法. 2000MS代码: #include <cstdio> #include ...

  2. 基本数据类型 list and tuple 04

    列表和元组 一,列表 1.列表 由[]括起来 可以存放各种数据类型:  存放量比较大 2.列表的索引和切片  列表也有索引 lst [i] i 即列表中各元素的位置 2.1列表的切片 lst[star ...

  3. 【ACM】小猴子下落

    小猴子下落 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 有一颗二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从左到右从上到下的编号为1,2,3,····· ...

  4. JOIN 和 NULL

    NULL值得数据出现在数据库发展的最初阶段的确给开发和使用者带来了很大的便利,这是因为它为我们节省了太多的磁盘空间,而且在那个年代磁盘是相当昂贵的.但是随着科技的发展,硬件系统的改进突飞猛进,NULL ...

  5. 转 from __future__ import unicode_literals

    转自 https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868200230 ...

  6. OfficeControl插件的用法

    项目中需要用到文档在线编辑的功能,网上找到这篇文章: http://hi.baidu.com/hurtingwings/item/bf83b6343305a94e3075a19e

  7. vue proxyTable 接口跨域请求调试(五)

    在不同域之间访问是比较常见,在本地调试访问远程服务器....这就是有域问题. VUE解决通过proxyTable: 在 config/index.js 配置文件中 dev: { env: requir ...

  8. mysql 死锁解决办法

    查询表的时候,发现一圈圈转啊转,就是不出来数据,猜测表被锁住 解决办法 : mysql> show processlist ; mysql> kill 4;       说明 : 4为 i ...

  9. Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) F. Substrings in a String

    http://codeforces.com/contest/914/problem/F 以前做过一个类似的,但是查询的子串长度最多是10,这个时候就是用bit + hash做了.这是因为改变一个字符, ...

  10. LoadScene场景异步加载

    LoadScene场景异步加载 using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; usin ...