题面

考虑双向广搜。

我们需要记录男孩和女孩的当前位置,并且每次都进行扩展。

记录一个数组 \(st[i][j]\) 。

  • 如果 \(st[i][j]=0\) ,说明 \((i,j)\) 还没有被男孩和女孩经过;
  • 如果 \(st[i][j]=1\) ,说明 \((i,j)\) 被男孩经过了;
  • 如果 \(st[i][j]=2\) ,说明 \((i,j)\) 被女孩经过了。

如果当前扩展的是男孩,且当前的位置是 \((x,y)\) ,那么当 \(st[x][y]=2\) 时直接返回当前扩展的层数即可;

如果当前扩展的是女孩,那么当 \(st[x][y]=1\) 时直接返回就可以了。

题目中有一句话:

在第 \(k\) 秒后所有与鬼的曼哈顿距离不超过 \(2k\) 的位置都会被鬼占领。

我们根据这句话的提示判断该点是否要扩展即可。

其实双队列来实现双向广搜还是比较易懂的。

#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d\n", __FUNCTION__, __LINE__)
#define itn int
#define gI gi using namespace std; inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
} const int maxn = 803; int t, n, m;
char s[maxn][maxn];
int st[maxn][maxn];
pair <int, int> boy, girl, ghost[5]; const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; inline bool check(int x, int y, int step)
{
if (x <= 0 || x > n || y <= 0 || y > n || s[x][y] == 'X') return false; //出界了或者走到了墙
for (int i = 1; i <= 2; i+=1)
if (abs(x - ghost[i].first) + abs(y - ghost[i].second) <= step * 2) return false; //判断与鬼的曼哈顿距离
return true;
} inline int bfs()
{
memset(st, 0, sizeof(st)); //记得数组初始化
queue <pair <int, int> > qb, qg;
int tot = 0;
for (int i = 1; i <= n; i+=1)
{
for (int j = 1; j <= m; j+=1)
{
if (s[i][j] == 'M') boy = (make_pair)(i, j);
else if (s[i][j] == 'G') girl = (make_pair)(i, j);
else if (s[i][j] == 'Z') ghost[++tot] = (make_pair)(i, j);
//记录男孩、女孩和鬼的位置
}
}
qb.push(boy); qg.push(girl);
int step = 0; //扩展的层数
while (qb.size() || qg.size()) //如果还可以扩展
{
++step;
for (int i = 1; i <= 3; i+=1) //男孩扩展 3 步
{
for (int j = 1, len = qb.size(); j <= len; j+=1) //要对队列中的每一个元素进行扩展
{
pair <int, int> p = qb.front(); qb.pop();
int x = p.first, y = p.second;
if (!check(x, y, step)) continue;
for (int k = 0; k < 4; k+=1)
{
int xx = x + dx[k], yy = y + dy[k];
if (!check(xx, yy, step)) continue;
if (st[xx][yy] == 2) return step;
if (!st[xx][yy])
st[xx][yy] = 1, qb.push((make_pair)(xx, yy));
}
}
}
for (int i = 1; i <= 1; i+=1) //女孩只要扩展一步
{
for (int j = 1, len = qg.size(); j <= len; j+=1)
{
pair <int, int> p = qg.front(); qg.pop();
int x = p.first, y = p.second;
if (!check(x, y, step)) continue;
for (int k = 0; k < 4; k+=1)
{
int xx = x + dx[k], yy = y + dy[k];
if (!check(xx, yy, step)) continue;
if (st[xx][yy] == 1) return step;
if (!st[xx][yy])
st[xx][yy] = 2, qg.push((make_pair)(xx, yy));
}
}
}
}
return -1; //无解
} int main()
{
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
t = gi();
while (t--)
{
n = gi(), m = gi();
for (int i = 1; i <= n; i+=1) scanf("%s", s[i] + 1);
printf("%d\n", bfs());
}
return 0;
}

题解【AcWing177】噩梦的更多相关文章

  1. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. 【CSP-S膜你考】不怕噩梦 (模拟)

    不怕噩梦 题面 蚊子最近经常做噩梦,然后就会被吓醒.这可不好.. 疯子一直在发愁,然后突然有一天,他发现蚊子其实就是害怕某些事. 如果那些事出现在她的梦里,就会害怕. 我们可以假定那个害怕的事其实是一 ...

  3. [CQOI2012]模拟工厂 题解(搜索+贪心)

    [CQOI2012]模拟工厂 题解(搜索+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327574 链接题目地址:洛谷P3161 BZOJ P26 ...

  4. 洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)

    洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/132 ...

  5. web前端的春天 or 噩梦

    「 微信应用号可以做什么」 简单说,微信"小程序"可以为开发者提供基于微信的表单.导航.地图.媒体和位置等开发组件,让他们在微信的网页里构建一个 HTML 5 应用.同时微信还开放 ...

  6. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  7. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  8. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  9. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

随机推荐

  1. JavaScript中条件分支语句和循环语句的使用,用简洁的代码实现强大功能

    if()  else if()   else() alert() 弹出警告框 prompt() 输入框,确定:返回输入信息:取消:返回null <!DOCTYPE html> <ht ...

  2. tomcat - 解决 org.bouncycastle.asn1.ASN1Boolean 非法循环依赖的错误

    背景 记录遇到一次奇怪的错误,在发布war包到Tomcat的时候,出现了org.bouncycastle.asn1.ASN1Boolean非法循环依赖的错误. INFO: Deploying web ...

  3. 20200228 尚硅谷-NIO

    尚硅谷-NIO Java NIO简介 Java NIO(New IO.Non Blocking IO)是从Java1.4版本开始引入的新的 IO API,可以替代标准的 Java IO API. NI ...

  4. 在服务器上安装并配置JDK1.8

    参考链接:https://blog.csdn.net/qq_40958000/article/details/83996912

  5. 使用 Express 脚手架

    安装: npm install -g express-generator 创建项目: express myapp 安装依赖 install dependencies: > npm install ...

  6. Spark学习之路 (六)Spark Transformation和Action[转]

    Transformation算子 基本的初始化 (1)java static SparkConf conf = null; static JavaSparkContext sc = null; sta ...

  7. 如何实现 List 集合的线程安全

    在实际项目开发中,List 集合较为常用,相比于数组,其提供了更多的方法,便于遍历.搜索.添加与移除.常见的有 ArrayList.Vector等.   关于ArrayList 中所周知,ArrayL ...

  8. 对主定理(Master Theorem)的理解

    前言 虽说在学OI的时候学到了非常多的有递归结构的算法或方法,也很清楚他们的复杂度,但更多时候只是能够大概脑补这些方法为什么是这个复杂度,而从未从定理的角度去严格证明他们.因此借着这个机会把主定理整个 ...

  9. JS编解码与Java编解码的对应关系

    最近前段在导出数据时会遇到“illegal character”的异常错误,结果发现是在请求地址中请求参数包含了空白字符(其编码为%C2%A0)或者是空格字符(其编码为%20),之前对空格字符情况是做 ...

  10. Leetcode Week3 Merge Two(k) Sorted Lists

    Question Q1.Merge two sorted linked lists and return it as a new list. The new list should be made b ...