题目描述

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;
}

转载于:https://www.cnblogs.com/zxyqzy/p/10328118.html

[USACO07DEC]泥水坑Mud Puddles BFS BZOJ 1627相关推荐

  1. bzoj 1627: [Usaco2007 Dec]穿越泥地(BFS)

    1627: [Usaco2007 Dec]穿越泥地 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 768  Solved: 517 [Submit][S ...

  2. 如何种植屡获殊荣的青豆

    Most people don't know this yet, but I've decided to give up computers and become a farmer instead. ...

  3. 搜索 —— 广搜的优化技巧

    [例题] 连连看(HDU-1175)(简单推导):点击这里 字串变换(洛谷-P1032)(string的使用):点击这里 非常可乐(HDU-1495)(三维BFS):点击这里 Mud Puddles( ...

  4. Mach 微内核的命名趣闻

    据 Tevanian 说,MACH,源于一个读音错误.当时他正和其他人在匹兹堡一个下雨天里一边躲避着路上的泥水坑,一边讨论着新内核的事,Tevanian开玩笑地建议他们的新微核命名为MUCK,意为&q ...

  5. 谁在控制着 iCloud 中国区账号的密钥?

    点击上方"CSDN",选择"置顶公众号" 关键时刻,第一时间送达! 还有一天的时间,我们的 iCloud 数据就要被苹果正式转给云上贵州运营了... [CSDN ...

  6. iOS 探讨之 mach_absolute_time

    阐述 去年在与大厂的交手中,碰到了如何测量函数耗时这类的问题.最近正好有点空余时间供我研究并整理它们,希望这次整理出的东西能给大家带来一点帮助. 探讨 测量函数耗时所用的方法有很多种,这次我们先来介绍 ...

  7. 2023全国特种作业操作证高处安装、维护、拆除模拟试卷一[安考星]

    该模拟试题来源于安考星公众号 1.安装室外机尽量避开对面的门窗. 正确答案:正确 参考解析:这是为了尽量避开对流. 2.多种施工机械和电源电器必须由持证人员操作,无证人员可酌情开机和接电 正确答案:错 ...

  8. 2020年数据术语的故事

    点击上方蓝色字体,选择"设为星标" 回复"资源"获取更多资源 2020年整个技术圈子要说话题最多的,应该是大数据方向.新感念层出不穷,数据湖概念就是其中之一.这 ...

  9. [转]在10岁前要做的32件事!

    现在的儿童很少有时间来自娱自乐,原因是,有些父母担心孩子的安全:不希望他们的衣服被搞得脏兮兮的:另一些孩子的日程被排得过满,他们不是去上课,就是去参加有组织的课余活动. 为使宝宝能够重新拾起他们父母孩 ...

  10. 2019肇庆学院“菜鸟杯“程序设计竞赛题解

    A 解锁专家 阿炳是一个精通文理的小机灵鬼,它是一个解锁专家,也是一个诗人.一天,阿炳受邀前往黄台甫马哈那坤弃他哇劳狄希阿由他亚马哈底陆浦欧叻辣塔尼布黎隆乌冬帕拉查尼卫马哈洒坦,也就是今天俗称的曼谷, ...

最新文章

  1. Oracle创建用户设置权限
  2. Acwing第 6 场周赛【未完结】
  3. 牛客多校第六场 E Androgynos 自补图
  4. Android开发的之基本控件和详解四种布局方式
  5. 关于.c和.h 和定义变量的问题
  6. 莫桑比克wcdma频段_开放医疗记录社区支持莫桑比克的新系统
  7. Myeclipse学习总结(7)——Eclipse插件之Maven配置及问题解析
  8. 链接ftp,把文件或图片上传到ftp指定的文件夹中
  9. Boostnote跨平台 Markdown 编辑器
  10. java beanutil 工具类_Apache Commons BeanUtils PropertyUtils工具类操作Java Bean属性
  11. 达科为在创业板递交注册申请:拟募资8亿元,吴庆军父女为实控人
  12. VS2019 无法登录 许可证已过期 无法下载许可证
  13. Java面试通关神器,冲鸭,进大厂!
  14. 解决tomcat安装配置后localhost 打不开
  15. SQL注入测试平台 SQLol -2.SELECT注入测试
  16. java实现第四届蓝桥杯好好学习
  17. 南开大学计算机科学与技术研究生院,2021年南开大学计算机科学与技术(081200)硕士研究生招生信息_考研招生计划和招生人数 - 学途吧...
  18. 赵呆呆,Fight!
  19. 西北乱跑娃 --- windows下kill进程
  20. 计算机储存数据怎样操作,计算机如何存储数据

热门文章

  1. 【git】cherry-pick详解
  2. react 跳转外部链接
  3. 在星巴克聊聊“一找小七”
  4. 关于请求_小七_新浪博客
  5. 基于搜狗搜索的微信公众号爬虫实现(C#版本)
  6. docker容器获取宿主机ip地址
  7. when函数c语言,when表达式
  8. Win8.1屏幕亮度自动调节关闭方法
  9. 为什么《百家讲坛》上的中学教师收视率最高?
  10. vc中cout如何解除fixed控制_C++ fixed用法详解