[USACO12MAR]拖拉机
题目描述
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 才能将拖拉机开会坐标原点。
输入输出样例
思路
如果这也叫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]拖拉机的更多相关文章
- 洛谷 P1849 [USACO12MAR]拖拉机Tractor
题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...
- 洛谷—— P1849 [USACO12MAR]拖拉机Tractor
https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...
- [luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)
传送门 神奇的spfa #include <queue> #include <cstdio> #include <cstring> #include <ios ...
- NowCoderWannafly挑战赛5-可编程拖拉机比赛-向上取整和向下取整函数
可编程拖拉机比赛 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 "这个比赛,归根结底 ...
- 洛谷P2698 [USACO12MAR]花盆Flowerpot
P2698 [USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and n ...
- P2698 [USACO12MAR]花盆Flowerpot(单调队列+二分)
P2698 [USACO12MAR]花盆Flowerpot 一看标签........十分后悔 标签告诉你单调队列+二分了............ 每次二分花盆长度,蓝后开2个单调队列维护最大最小值 蓝 ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 牛客网 Wannafly挑战赛5 B.可编程拖拉机比赛-ceil()函数+floor()函数
可编程拖拉机比赛 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 “这个比赛,归根结底就是控制一 ...
随机推荐
- 第一章、 CLR的执行模型
1. 概述 本章主要是介绍从源代码到可执行程序的过程中,CLR所做的工作. 2. 名词解释 ① 公共语言运行时(Common Language Runtime, CLR),是一个可由多种语言使用的 运 ...
- mysql安装及基本概念
1.mysql下载安装 在官网下载5.6版本(越老稳定性越好,现在公司一般都用5.6),选择windows,64bit .下载完解压看bin目录下是否有mysql·exe和mysqld.exe. 解压 ...
- 洛谷P1251 餐巾计划问题(最小费用最大流)
题意 一家餐厅,第$i$天需要$r_i$块餐巾,每天获取餐巾有三种途径 1.以$p$的费用买 2.以$f$的费用送到快洗部,并在$m$天后取出 3.以$s$的费用送到慢洗部,并在$n$天后取出 问满足 ...
- 保密安全风险自评估单机版检查工具V1.0
下载链接:http://download.csdn.net/download/moremoretea1983/10273128
- SEO 第八章
SEO第八章 本次课目标: 1. 网站外部优化的外链优化 2. 网站流量分析 1. 什么叫做外链? 外链也叫反向链接,指的是从别的网站指向我自己的网站的链接. 2. 外链的作用? l 外链可 ...
- javaee 第14周
1.web server Web Server中文名称叫网页服务器或web服务器.WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务.Web服务器可以解析( ...
- Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)
转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...
- while循环(break、continue)
while循环 流程:判断条件是否为真,如果条件为真,执行代码块,然后再次判断条件是否为真,如果为真,执行代码块,直到条件判断为假,结束循环 格式 while 条件: 代码块(循环体) else:- ...
- WebGL 绘制Line的bug(三)
之前铺垫了许多,今天可以来分享点纯干货了. 上一篇已经讲述了通过面模拟线条时候,每一个顶点的顶点数据包括:端点坐标.偏移量.前一个端点坐标.后一个端点坐标,当然如果我们通过索引的方式来绘制的话,还包括 ...
- vue 父子组件的加载顺序
一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...