题目描述

Farmer John is leaving his house promptly at 6 AM for his daily milking of Bessie. However, the previous evening saw a heavy rain, and the fields are quite muddy. FJ starts at the point (0, 0) in the coordinate plane and heads toward Bessie who is located at (X, Y) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (1 ≤ N ≤ 10,000) puddles of mud, located at points (Ai, Bi) (-500 ≤ Ai ≤ 500; -500 ≤ Bi ≤ 500) on the field. Each puddle occupies only the point it is on.

Having just bought new boots, Farmer John absolutely does not want to dirty them by stepping in a puddle, but he also wants to get to Bessie as quickly as possible. He's already late because he had to count all the puddles. If Farmer John can only travel parallel to the axes and turn at points with integer coordinates, what is the shortest distance he must travel to reach Bessie and keep his boots clean? There will always be a path without mud that Farmer John can take to reach Bessie.

清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地。FJ的屋子在平面坐标(0, 0)的位置,贝茜所在的牛棚则位于坐标(X,Y) (-500 <= X <= 500; -500 <= Y <= 500)处。当然咯, FJ也看到了地上的所有N(1 <= N <= 10,000)个泥塘,第i个泥塘的坐标为 (A_i, B_i) (-500 <= A_i <= 500;-500 <= B_i <= 500)。每个泥塘都只占据了它所在的那个格子。 Farmer John自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果Farmer John 只能平行于坐标轴移动,并且只在x、y均为整数的坐标处转弯,那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。

输入输出格式

输入格式:

  • Line 1: Three space-separate integers: X, Y, and N.

  • Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

输出格式:

  • Line 1: The minimum distance that Farmer John has to travel to reach Bessie without stepping in mud.

输入输出样例

输入样例#1: 复制

1 2 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2
输出样例#1: 复制

11
思路:宽搜
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int sx,sy,n,flag,ans=0x7f7f7f7f;
int tx[]={,-,,};
int ty[]={,,,-};
int map[][];
struct nond{
int x,y,dis;
};
int main(){
scanf("%d%d%d",&sx,&sy,&n);
sx+=;sy+=;
for(int i=;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
map[x+][y+]=;
}
queue<nond>que;
que.push((nond){,,});
map[][]=;
while(!que.empty()){
nond now=que.front();
que.pop();
for(int i=;i<;i++){
int dx=now.x+tx[i];
int dy=now.y+ty[i];
if(dx<||dy<||map[dx][dy]) continue;
if(dx==sx&&dy==sy){ ans=now.dis+;flag=;break; }
map[dx][dy]=;
que.push((nond){dx,dy,now.dis+});
}
if(flag) break;
}
cout<<ans;
}
 

洛谷 P2873 [USACO07DEC]泥水坑Mud Puddles的更多相关文章

  1. bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

    P2873 [USACO07DEC]泥水坑Mud Puddles bfs入门. 对于坐标为负的情况,我们可以给数组下标加上$abs(min(minx,miny))$转正(根据题意判断) #includ ...

  2. [USACO07DEC]泥水坑Mud Puddles BFS BZOJ 1627

    题目描述 Farmer John is leaving his house promptly at 6 AM for his daily milking of Bessie. However, the ...

  3. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows

    一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...

  4. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...

  5. 洛谷P2870 - [USACO07DEC]最佳牛线Best Cow Line

    Portal Description 给出一个字符串\(s(|s|\leq3\times10^4)\),每次从\(s\)的开头或结尾取出一个字符接在新字符串\(s'\)的末尾.求字典序最小的\(s'\ ...

  6. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  7. 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  8. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

    题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...

  9. 洛谷 P2871 [USACO07DEC]手链Charm Bracelet 题解

    题目传送门 这道题明显就是个01背包.所以直接套模板就好啦. #include<bits/stdc++.h> #define MAXN 30000 using namespace std; ...

随机推荐

  1. 20.发送http请求服务 ($http)

    转自:https://www.cnblogs.com/best/tag/Angular/ 服务从代码直接与服务器进行交互,底层是通过实现,与中http服务从AngularJS代码直接与Web服务器进行 ...

  2. jquery ajax中支持哪些返回类型以及js中判断一个类型常用的方法?

    1 jquery ajax中支持哪些返回类型在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 预期服务器返回的数据类型.如果不指定,jQuery 将自 ...

  3. BZOJ 4003 左偏树

    思路: 用到了左偏树合并复杂度是logn的性质 一开始先BFS一遍 打标记的左偏树 //By SiriusRen #include <cstdio> #include <cstrin ...

  4. Gym - 100685F Flood BFS

    Gym - 100685F 题意:n个水池之间流水,溢出多少流出多少,多个流出通道的话平均分配,给你每个水池中的水量和容量,问到最后目标水池中水量. 思路:直接用队列扩展,不过这里有一个优化,就是统计 ...

  5. asp.net MVC4.0中几种控制器的区别

    空的MVC控制器MVC控制器的读/写操作和视图,使用实体框架MVC控制器带空的读/写操作空API控制器API控制器的读/写操作和视图,使用实体框架API控制器带空的读/写操作

  6. 分享一下js正则中惰性与贪婪

    首先引入一个介绍比较详细的网站 http://www.cnblogs.com/yuaima/p/5258513.html http://www.jb51.net/article/31491.htm 接 ...

  7. SQL保存XML报错 “XML 分析: 行 1,字符 47,非法的 xml 字符”

    例如: <?xml version="1.0" encoding="utf-8" standalone="yes"?> < ...

  8. 10G安装DataGuard

    最后更新时间:2013年8月4日,星期日 ★ oracle 10G安装环境 数据库软件安装环境不详细描述,网上到处有这方面资料,下面只简单描述下. 也可参考官方文档: http://docs.orac ...

  9. rm---删除目录huo文件

    rm命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉.对于链接文件,只是删除整个链接文件,而原有文件保持不变. 注意:使用rm命令要格外小心.因为一旦 ...

  10. 【Henu ACM Round#18 A】 Multiplication Table

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 遍历i从1..n 看看x%i==0以及x/i<=n是否成立. [代码] #include <iostream> u ...