题目描述

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. modSecurity规则学习(八)——防止CC攻击

    modSecurity日志收集:在phase 5阶段处理. 由于CC攻击主要考虑对动态请求的防护,所以要排除静态资源的请求,或者自定义动态请求的后缀或者关键字做接口针对性的防护. 定义需要排除的请求u ...

  2. Gitblit从一个服务器,迁移到另外一个服务器

    http://gitblit.com/federation.html A Gitblit federation is a mechanism to clone repositories and kee ...

  3. 15.Intellij中配置jdk/tomcat/maven

    转自:https://blog.csdn.net/u010414666/article/details/44465905 继上一篇安装好了Intellij之后,我们可以对Intellij,做一些简单的 ...

  4. Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库

    本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...

  5. codeforces 710A King Moves(水)

    output standard output The only king stands on the standard chess board. You are given his position ...

  6. Python(二) 表示‘组’的概念与定义

    现实世界中总存在一组一组的事物, 一.列表的定义 type(['hello','world',1,9,True,False]) = <class 'list'> type([[1,2,3, ...

  7. Chrome OS 70 发布:这是安卓的私生子吗?

    谷歌于28日正式宣布推出Chrome OS 70.这个最新的Chrome OS系统在一些设计上具备了更多安卓风味,为配备了触摸屏的Chromebook.平板电脑和二合一设备带来了操作界面改善. 据9t ...

  8. route---设置Linux内核中的网络路由表

    route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现. 在L ...

  9. 【Uva 1601】The Morning after Halloween

    [Link]: [Description] 给你一张平面图; 最多可能有3只鬼; 给出这几只鬼的初始位置; 然后,这几只鬼有各自的终点; 每秒钟,这几只鬼能同时移动到相邻的4个格子中的一个 任意两只鬼 ...

  10. 从零使用qemu模拟器搭建arm执行环境

    为什么会有这篇文章 早在2011年的时候,跟当时同事一起讨论,做Linux系统开发正处于整个Linux开发中间层,没有上层的C/C++业务和数据库的开发经验.也没有底层的内核和驱动开发经验,究竟路该怎 ...