题目链接:https://ac.nowcoder.com/acm/contest/993/F/
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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.

输入描述

* 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 2 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2

输出

11

说明

Bessie is at (1, 2). Farmer John sees 7 mud puddles, located at (0, 2); (-1, 3); (3, 1); (1, 1); (4, 2); (-1, 1) and (2, 2).
The best path for Farmer John is (0, 0); (-1, 0); (-2, 0); (-2, 1); (-2, 2); (-2, 3); (-2, 4); (-1, 4); (0, 4); (0, 3); (1, 3); and (1, 2), finally reaching Bessie.

解题思路

题意:求(0, 0)到(x, y)的最小距离。
思路:直接BFS搜索就行了,注意有负数,对x和y都扩大500。

Accepted Code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int MAXM = 1000;
struct edge {int x, y, t;
}p;
bool vis[MAXN][MAXN];
int arr[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int BFS(int x, int y) {queue <edge> Q;vis[500][500] = true;Q.push((edge){500, 500, 0});while (!Q.empty()) {p = Q.front();Q.pop();if (!(p.x - x - 500) && !(p.y - y - 500))return p.t;for (int i = 0; i < 4; i++) {int tx = p.x + arr[i][0];int ty = p.y + arr[i][1];if (tx >= 0 && tx <= MAXM && ty >= 0 && ty <= MAXM && !vis[tx][ty]) {Q.push((edge){tx, ty, p.t + 1});vis[tx][ty] = true;}}}return 0;
}
int main() {int x, y, u, v, n;scanf("%d%d%d", &x, &y, &n);for (int i = 0; i < n; i++) {scanf("%d%d", &u, &v);vis[u + 500][v + 500] = true;}printf("%d\n", BFS(x, y));return 0;
}

牛客网 - [牛客假日团队赛6]Mud Puddles(BFS)相关推荐

  1. 牛客假日团队赛8:F.Telephone Lines(二分+spfa)

    链接:https://ac.nowcoder.com/acm/contest/1069/F 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  2. 牛客假日团队赛8:H.Cell Phone Network(最小支配集)

    链接:https://ac.nowcoder.com/acm/contest/1069/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  3. 牛客假日团队赛8:K.Cow Contest(最短路(floyd)变形)

    链接:https://ac.nowcoder.com/acm/contest/1069/K 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  4. 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)...

    链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  5. 牛客假日团队赛10 L 乘积最大 (dp,大数)

    链接:https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm 来源:牛客网 乘积最大 时间限制:C/C+ ...

  6. P5200 [USACO19JAN]Sleepy Cow Sorting 牛客假日团队赛6 D 迷路的牛 (贪心)

    链接:https://ac.nowcoder.com/acm/contest/993/E 来源:牛客网 对牛排序 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  7. 牛客假日团队赛6 D 迷路的牛 (思维)

    链接:https://ac.nowcoder.com/acm/contest/993/D 来源:牛客网 迷路的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  8. 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)...

    链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  9. 牛客假日团队赛5 K 金币馅饼 (DP 基础题)

    链接:https://ac.nowcoder.com/acm/contest/984/K 来源:牛客网 金币馅饼 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  10. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

最新文章

  1. GVINS:基于GNSS视觉惯性紧耦合的平滑状态估计方法
  2. WCF实现RESTFul Web Service
  3. 战神背光键盘如何关系_?复工了?换个键盘先!0-2000元键盘推荐
  4. 七骑士android版上线时间,腾讯独代韩手游《七骑士》今日全面公测
  5. 6 areas of artificial intelligence to watch closely 需要密切关注的六大人工智能/机器学习领域
  6. Shell 获取进程号
  7. ETL、BI、MMP数据库
  8. 病毒木马查杀实战第025篇:JS下载者脚本木马的分析与防御
  9. 即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
  10. python ——XML操作
  11. docker 配置远程deamon
  12. 基本知识 100048
  13. 编程实现linux中的who命令功能,Linux who命令简介及使用方法详解
  14. 人大金仓数据库(KingbaseES)帮助文档
  15. 【单目标优化求解】基于matlab黑猩猩算法求解单目标问题【含Matlab源码 1413期】
  16. nginx:nginx学习
  17. 风控建模三:变量筛选原则
  18. 设备加密狗、软加密狗
  19. 做PPT用到的实用且免费的网站
  20. [华中科技计组实验]logisim完成单周期5级流水MIPS CPU

热门文章

  1. 海外问卷调查项目分为哪几种?
  2. 安装易飞ERP打开出错的两例
  3. Volatile关键字~转载自博客园的“海子”
  4. 函数的支集、支撑集、support、supp
  5. 数据预处理——样本分布(正态分布、偏态分布)
  6. FinClip手把手教学:如何从零开始编写一个app
  7. MySQL把性别编码转换成名字_两种转换mysql数据编码的方法
  8. Asp.net MVC 填充word并下载
  9. 线程main java中的异常怎么解决_线程“ main”中的异常java.lang.NoClassDefFoundError:...
  10. Pycharm一直显示connecting to console