AcWing:177. 噩梦(bfs)
给定一张N*M的地图,地图中有1个男孩,1个女孩和2个鬼。
字符“.”表示道路,字符“X”表示墙,字符“M”表示男孩的位置,字符“G”表示女孩的位置,字符“Z”表示鬼的位置。
男孩每秒可以移动3个单位距离,女孩每秒可以移动1个单位距离,男孩和女孩只能朝上下左右四个方向移动。
每个鬼占据的区域每秒可以向四周扩张2个单位距离,并且无视墙的阻挡,也就是在第k秒后所有与鬼的曼哈顿距离不超过2k的位置都会被鬼占领。
注意: 每一秒鬼会先扩展,扩展完毕后男孩和女孩才可以移动。
求在不进入鬼的占领区的前提下,男孩和女孩能否会合,若能会合,求出最短会合时间。
输入格式
第一行包含整数T,表示共有T组测试用例。
每组测试用例第一行包含两个整数N和M,表示地图的尺寸。
接下来N行每行M个字符,用来描绘整张地图的状况。(注意:地图中一定有且仅有1个男孩,1个女孩和2个鬼)
输出格式
每个测试用例输出一个整数S,表示最短会合时间。
如果无法会合则输出-1。
每个结果占一行。
数据范围
1<n,m<8001<n,m<800
输入样例:
3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...
10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X
输出样例:
1
1
-1
算法:bfs
题解:就是先枚举鬼能走到的位置,然后在枚举女孩和男孩要走到的位置,重复做这件事,直到女孩能碰到男孩,否则输出-1.因为它走过的次数不会超过1e5次(读者可以自己证明)。
#include <iostream>
#include <cstdio>
#include <queue> using namespace std; const int maxn = 1e3+; struct node {
int x, y, step;
}; char Map[maxn][maxn];
int dir[][] = {, -, , , -, , , };
int n, m; bool check(int x, int y) {
if(x <= || x > n || y <= || y > m) {
return false;
}
return true;
} int bfs() {
queue<node> girl, boy, ghost;
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(Map[i][j] == 'M') {
boy.push((node){i, j, });
} else if(Map[i][j] == 'G') {
girl.push((node){i, j, });
} else if(Map[i][j] == 'Z') {
ghost.push((node){i, j, });
}
}
}
for(int i = ; i <= ; i++) { //枚举来遍历这些时间
int tmp = ghost.front().step + ; //限制了移动的距离
while(!ghost.empty() && ghost.front().step < tmp) {
node now = ghost.front();
ghost.pop();
for(int j = ; j < ; j++) {
int tx = now.x + dir[j][];
int ty = now.y + dir[j][];
if(check(tx, ty) && Map[tx][ty] != '#') {
ghost.push((node){tx, ty, now.step + });
Map[tx][ty] = '#';
Map[now.x][now.y] = '#';
}
}
}
tmp = boy.front().step + ;
while(!boy.empty() && boy.front().step < tmp) {
node now = boy.front();
boy.pop();
for(int j = ; j < ; j++) {
int tx = now.x + dir[j][];
int ty = now.y + dir[j][];
if(check(tx, ty) && Map[tx][ty] != '#') {
if(Map[tx][ty] == '.') {
boy.push((node){tx, ty, now.step + });
Map[tx][ty] = 'M';
Map[now.x][now.y] = 'X';
} else if(Map[tx][ty] == 'G') {
return i;
}
}
}
}
tmp = girl.front().step + ;
while(!girl.empty() && girl.front().step < tmp) {
node now = girl.front();
girl.pop();
for(int j = ; j < ; j++) {
int tx = now.x + dir[j][];
int ty = now.y + dir[j][];
if(check(tx, ty) && Map[tx][ty] != '#') {
if(Map[tx][ty] == '.') {
girl.push((node){tx, ty, now.step + });
Map[tx][ty] = 'G';
Map[now.x][now.y] = 'X';
} else if(Map[tx][ty] == 'M') {
return i;
}
}
}
} }
return -;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) {
getchar();
for(int j = ; j <= m; j++) {
scanf("%c", &Map[i][j]);
}
}
printf("%d\n", bfs());
}
return ;
}
AcWing:177. 噩梦(bfs)的更多相关文章
- Leapin' Lizards(hdu 2732)
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 借助Photoshop,Illustrator等设计软件进行WPF图形图像的绘制
原文:借助Photoshop,Illustrator等设计软件进行WPF图形图像的绘制 本文所示例子是借助第三方设计软件,制作复杂的矢量图形,转成与XAML酷似的SVG,再转换成xaml而实现的. 这 ...
- 噩梦(双向BFS)
给定一张N*M的地图,地图中有1个男孩,1个女孩和2个鬼. 字符“.”表示道路,字符“X”表示墙,字符“M”表示男孩的位置,字符“G”表示女孩的位置,字符“Z”表示鬼的位置. 男孩每秒可以移动3个单位 ...
- AcWing:176. 装满的油箱(bfs + dijiskla思想)
有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...
- AcWing:175. 电路维修(bfs)
达达是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女翰翰,从而被收留在地球上. 翰翰的家里有一辆飞行车. 有一天飞行车的电路板突然出现了故障,导致无法启动. 电路板的整体结构是一个R ...
- AcWing:173. 矩阵距离(bfs)
给定一个N行M列的01矩阵A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为: dist(A[i][j],A[k][l])=|i−k|+|j−l|dist(A[i][j],A[k][l]) ...
- AcWing:172. 立体推箱子(bfs)
立体推箱子是一个风靡世界的小游戏. 游戏地图是一个N行M列的矩阵,每个位置可能是硬地(用”.”表示).易碎地面(用”E”表示).禁地(用”#”表示).起点(用”X”表示)或终点(用”O”表示). 你的 ...
- acwing 173. 矩阵距离(bfs)
给定一个N行M列的01矩阵A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为: dist(A[i][j],A[k][l])=|i−k|+|j−l|dist(A[i][j],A[k][l]) ...
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
随机推荐
- 解决go mod或go get时`x509: certificate signed by unknown authority`错误
一般go get私有仓库时会出现如下错误: go: xxx@v0.0.0-20190918102752-bb51b27911ca: unrecognized import path "xxx ...
- 工作单元 — Unit Of Work
在进行数据库添加.修改.删除时,为了保证事务的一致性,即操作要么全部成功,要么全部失败.例如银行A.B两个账户的转账业务.一方失败都会导致事务的不完整性,从而事务回滚.而工作单元模式可以跟踪事务,在操 ...
- sql server常用函数总结
1. 日期函数相关 日期格式格式化函数:),UpdateTime,) --第3个参数为是要转换成的日期的格式,不同的数字代表不同的格式: 日期加减函数: ,UpdateTime) --第一个参数是刻度 ...
- JavaSpring【三、Bean】
配置项 id bean的标识 class bean的类全名 scope bean的作用域 constructor-arg 构造注入 properties 设值注入 autowire 装配模式 lazy ...
- openresty获取nginx中的变量
在OpenResty中如何引用这些变量呢? 规则很简单, 如$remote_addr, 在OpenResty里面使用就是ngx.var.remote_adddr.
- String类-StringBuffer类-StringBuilder类的比较
package LC20130929; /** * 字符串处理类: StringBuffer ~~ StringBuilder 〉〉 String 效果一样但是,性能却大不一样! ...
- Python:出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 0: invalid continuation byte问题
我在导入一个csv文件的时候出现了一个问题 报错的内容是这样的: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in positio ...
- 牛客练习赛42 C 出题的诀窍 (贡献,卡常)
牛客练习赛42 C 出题的诀窍 链接:https://ac.nowcoder.com/acm/contest/393/C来源:牛客网 题目描述 给定m个长为n的序列a1,a2,-,ama_1 , a_ ...
- 在Visual Studio Code 运行 webpack ./src/main.js --output-filename ./dist/bundle.js --output-path . --mode development 提示 Module no t found:Error:Can't resolve' 'jquery' 是因为vs code还没安装jquery
在Visual Studio Code 运行 webpack ./src/main.js --output-filename ./dist/bundle.js --output-path . --mo ...
- html z-index
如果你的悬浮的div被其他遮挡 那应该是遮挡的元素与它不是同级 可以考虑放在同一个父级目录下