题目描述

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 400005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int X, Y, N;
struct node {
int x, y;
int stp;
}pt[maxn],cur;
int mp[1001][1001];
int ans = inf; int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 }; bool OK(int x, int y) {
if (x <= 1000 && x >= 0 && y <= 1000 && y >= 0)return true;
return false;
}
queue<node>q; void bfs() {
q.push(node{ 500,500,0 });
cur.x = -1; cur.y = -1;
while (!q.empty()) {
cur = q.front(); q.pop();
if (cur.stp > ans)continue;
if (mp[cur.x][cur.y])continue;
if (cur.x == X && cur.y == Y) {
ans = min(ans, cur.stp);
}
mp[cur.x][cur.y] = 1;
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (OK(nx, ny) && mp[nx][ny] != 1 && mp[nx][ny] != 2) {
q.push(node{ nx,ny,cur.stp + 1 });
}
}
}
} int main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
X = rd(); Y = rd(); N = rd();
X += 500; Y += 500;
for (int i = 1; i <= N; i++) {
pt[i].x = rd(); pt[i].y = rd();
pt[i].x += 500; pt[i].y += 500;
mp[pt[i].x][pt[i].y] = 2;
}
bfs();
cout << ans << endl;
return 0;
}

[USACO07DEC]泥水坑Mud Puddles BFS BZOJ 1627的更多相关文章

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

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

  2. 洛谷 P2873 [USACO07DEC]泥水坑Mud Puddles

    P2873 [USACO07DEC]泥水坑Mud Puddles 题目描述 Farmer John is leaving his house promptly at 6 AM for his dail ...

  3. 穿越泥地(mud) (BFS)

    问题 C: 穿越泥地(mud) 时间限制: 1 Sec  内存限制: 128 MB提交: 16  解决: 10[提交][状态][讨论版] 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工 ...

  4. 穿越泥地(mud)

    穿越泥地(mud) 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想象,FJ现在面对的 是一大片泥泞的土地.FJ ...

  5. BZOJ 1627: [Usaco2007 Dec]穿越泥地( BFS )

    BFS... --------------------------------------------------------------------------------------- #incl ...

  6. bzoj 1627: [Usaco2007 Dec]穿越泥地【bfs】

    在洛谷上被卡了一个点开了O2才过= = bfs即可,为方便存储,把所有坐标+500 #include<iostream> #include<cstdio> #include&l ...

  7. BZOJ_1627_[Usaco2007_Dec]_穿越泥地_(bfs)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1627 网格图,给出起点,终点,障碍,求最短路. 分析 简单的宽搜. #include < ...

  8. 【做题记录】USACO silver * 50(第一篇)

    由于我太菜,决定按照AC人数从小到大慢慢做. BZOJ开了权限号真的快了好多诶~ 29/50 1606: [Usaco2008 Dec]Hay For Sale 购买干草 背包dp 1610: [Us ...

  9. 【BZOJ】1627: [Usaco2007 Dec]穿越泥地(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1627 裸bfs不解释.. #include <cstdio> #include < ...

随机推荐

  1. java之yield(),sleep(),wait()区别详解-备忘笔记(转)

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  2. 6410在rvds下编译启动代码报错分析

    contains invalid call from '~PRES8' function to 'REQ8' function main RVDS编译出现contains invalid call f ...

  3. Java多线程-新特征-信号量Semaphore

    简介信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们能够正确.合理的使用公共资源. 概念Semaphore分为单值和多值两种,前者只能 ...

  4. json和pickle序列化模块

    一.json序列化模块 1.序列化:将内存数据转成字符串加以保存. 2.反序列化:将字符串转成内存数据加以读取. data = { '北京':{ '五道口':{ 'sohu':'引擎', } } } ...

  5. spring quartz 配置多个定时任务

    1.配置文件-quartz-1.7.3jar   spring版本为3.1.3jar <?xml version="1.0" encoding="UTF-8&quo ...

  6. Gym - 101128H:Sheldon Numbers

    题意 给你两个整数X和Y 问你在区间[X,Y]中,有多少数字的二进制满足ABAB或者A这种形式.A是某个数量的1,B是某个数量的0. 分析 因为数据规模很大,直接枚举x和y之间的数字然后判断会超时.所 ...

  7. Using native JSON

    文介绍了兼容ECMAScript 5 标准的原生JSON对象. 在不支持原生JSON对象的旧版本Firefox中,该如何处理JSON数据.请查看 JSON. 原生JSON对象包含有两个关键方法.JSO ...

  8. linux上mysql安装

    所有平台的MySQL下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. MySQL - MySQL服务器.你需要该选项,除非你只想连接运行 ...

  9. Part8-不用内存怎么行_我从内部看内存lesson1

  10. AIO和NIO的理解

    AIO: AIO 背后的基本思想是允许进程发起很多 I/O 操作,而不用阻塞或等待任何操作完成,可以继续做 另外的事情,等I/O操作完成,内核会通过函数回调或者信号机制通知用户进程.这样很大程度提高了 ...