洛谷 P1849 [USACO12MAR]拖拉机Tractor
题目描述
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 才能将拖拉机开会坐标原点。
输入输出样例
7 6 3
6 2
5 2
4 3
2 1
7 3
5 4
6 4
1
#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的更多相关文章
- 洛谷—— P1849 [USACO12MAR]拖拉机Tractor
https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...
- 洛谷P2698 [USACO12MAR]花盆Flowerpot
P2698 [USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and n ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]
题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...
- 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- [luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)
传送门 神奇的spfa #include <queue> #include <cstdio> #include <cstring> #include <ios ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
随机推荐
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_NGen.exe
NGen.exe:本地代码生成器. [作用] 加快应用程序的启动速度 减小应用程序的工作集 [缺点] 没知识产权保护 生成的文件不能及时同步 执行时性能较差 [建议] 客户端考虑使用
- Windows任务计划创建计划,定时执行PowerShell命令
[环境介绍] 操作系统:Windows Server 2012 R2,64位操作系统 PowerShell版本:PowerShell 1.0 脚本位置:C:\BackUp.ps1 启动目录:C:\Wi ...
- http 和 https 的区别
参考:http://www.cnblogs.com/wqhwe/p/5407468.html HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从W ...
- Java 类型转换(int->String)
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- 新磁盘创建lvm并挂载
### .查看硬盘 fdisk -l ### 删除分区 fdisk /dev/sdc ### 按d删除,按w保存并退出 ### 创建pv pvcreate /dev/sdc ### 创建 vg vgc ...
- docker基本命令日志
docker run - Run a command in a new container 启动一个新的容器,一般在docker pull之后首次运行此image -i 保持stdout打开 -t 打 ...
- windows查看网络常用cmd命令
一.ping 主要是测试本机TCP/IP协议配置正确性与当前网络现状. ping命令的基本使用格式是: ping IP地址/主机名/域名 [-t] [-a] [-n count] [-l size] ...
- python搭配selenium,htmltestrunner实现自动化测试 —— (测试思路和基础步骤)
1. 测试思路: 编写测试单例 编写测试套件,集合测试单例 集中测试测试套件 生成测试报告 补充,发送测试结果到E-mail 2. 示例 编写测试单例 编写测试套件 测试脚本程序 生成报告 发送邮件 ...
- 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)
问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...
- 经典网络LeNet5看卷积神经网络各层的维度变化
本文介绍以下几个CNN经典模型:Lenet(1986年).Alexnet(2012年).GoogleNet(2014年).VGG(2014年).Deep Residual Learning(2015年 ...