hdu3085Nightmare Ⅱ(双向BFS)

HDU 3085 Nightmare Ⅱ(双向BFS) 题意   A、B在迷宫中,A每回合能走3步,B每回合能走1步,迷宫中还有2个鬼,每回合最先活动,会向两步内的格子分裂,A、B不能碰鬼,求他们最快见面的回合数 思路 ​ 题目中描述A能走3步,实际上意思是A每回合能移动[0,3]步,B也同理,所以只要考虑A、B尽可能最远到达的区域,在当前回合这些区域内都是可达的   将整个题目可以考虑为一个像染色的模型,A、B、鬼分别以每回合3、1、2的速度染色,... 阅读详情

Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4397 Accepted Submission(s): 1269

Problem Description
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.

Input
The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.

Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.

Sample Input
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

Sample Output
1
1
-1

Author
二日月

Source
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest

Recommend
lcy

双向bfs,124msAC

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
    node(){}
    node(int a,int b){x=a,y=b;}
};
char mp[805][805];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
vector<node>zp;
queue<node>mq[2];
int n,m,ans,tim,f;
int cnt[5];
int no(int x,int y){
    if(x<1||x>n||y<1||y>m||mp[x][y]=='X')return 1;
    if(abs(x-zp[0].x)+abs(y-zp[0].y)<=2*tim)return 1;
    if(abs(x-zp[1].x)+abs(y-zp[1].y)<=2*tim)return 1;
    return 0;
}
void bfs(int id){
    int siz=mq[id].size();
    while(siz--){
        node q=mq[id].front();
        mq[id].pop();
        if(no(q.x,q.y))continue;
        for(int i=0;i<4;i++){
            int nx=q.x+dx[i];
            int ny=q.y+dy[i];
            if(no(nx,ny))continue;
            if(mp[nx][ny]==(id^1)){f=1;return ;}
            if(mp[nx][ny]==id)continue;
            mp[nx][ny]=id;
            mq[id].push(node(nx,ny));
        }
    }
}
void solve(node st,node ed){
    ans=-1;f=0;tim=0;
    while(!mq[0].empty())mq[0].pop();
    while(!mq[1].empty())mq[1].pop();
    mq[0].push(ed);mq[1].push(st);
    while(!(mq[0].empty()||mq[1].empty())){
        tim++;
        bfs(0);
        bfs(1);
        bfs(1);
        bfs(1);
        if(f){ans=tim;return ;}
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>mp[i]+1;
        node ep,gp;
        zp.clear();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='M'){ep=node(i,j);mp[i][j]=1;}
                if(mp[i][j]=='G'){gp=node(i,j);mp[i][j]=0;}
                if(mp[i][j]=='Z')zp.push_back(node(i,j));
            }
        }
        solve(ep,gp);
        cout<<ans<<endl;
    }
    return 0;
}

HDU 3085 Nightmare Ⅱ【双向bfs】 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3927    Accepted Submission(s): 1131   Problem Description Last night, little er... 阅读详情

相关推荐

fuck girl magazines+18(3)

fuck girl magazines+18(3)病毒,可以直接运行,插入到你电脑你的U盘上的文件全部变成快捷方式!!!小心使用(删除不了,可以用策略组解决问题)

C. Little Girl and Maximum Sum Codeforces Round #169 (Div. 2)

http://codeforces.com/contest/276/problem/C 原题连接 解题说明:题目的意思是有若干对区间和的查询,问如何重组数组使查询结果的和最大。 我的解法就是将对应的频率排序之后在求(求频率那里需要点思维嘻嘻 直接贴代码了 #include <algorithm> #include <stack> #...

axian1654的博客 4268

HDU 3038 How Many Answers Are Wrong 带权并查集

一、内容 TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored). Then, FF can choose a continuous subsequence fro

js prototype 巨大疑惑

function FuckYou(){ } FuckYou.prototype =object_test = {     name:"John",  sex:'male',  getName:function(){     return this.name;  }

1792

C# chart以鼠标所在位置为中心进行缩放

以前都用labview做一些软件,但给出去总是会遇到版本,安装包过大等一些问题,于是想自学C#,记录一下使用过程中遇到的一些问题和解决措施,便于日后回顾。 在这个项目中需要绘制折线图,由于数据比较大,需要对图表进行缩放操作,以鼠标为中心对图表进行缩放,在网上查找了一些资料没找到我想要的,于是自己想了一个解决方案,可以基本实现,但略显繁琐,由于刚开始学习C#,对一些操作不太了解,若以后遇...

fei的博客 6363

Imatest-Dot Pattern

本文介绍了DotPattern模块测试方法,用于评估相机拍摄点阵图的表现。测试主要关注两点:色彩准确性(是否存在彩色噪点和杂边)和摩尔纹现象。测试步骤包括导入图片、选择分析区域、微调ROI、选择点状图类型和分析保存结果。测试结果包含横向色差、光学畸变和SMIATV畸变等数据,其中0.115%的色差值和0.004度的网格旋转角度表明相机表现良好,但-10.4%的SMIATV畸变显示存在明显桶形畸变。文章还提供了各项指标的具体解读标准。

weixin_64706696的博客 1351

The use of fuck: A sociolinguistic approach to the usage of fuck in the BNC and blog authorship 【翻译】

Acknowledgments Thank you to my advisor, Veronica, for allowing me to write and research this controversial topic. Also thank you to my family for supporting me throughout my education, and my friends...

hongfu951的专栏 2701

易智联LM401模块学习(四)——双串口收发

串口的发送和接受有三种方式,分别是阻塞发送和接收(轮询)、中断方式发送和接收、DMA方式发送和接收。这里我使用阻塞+中断的方式实现双串口通信。简单的串口收发。

weixin_46046597的博客 990

Eighth season fifteenth episode,Joey fell in love with Rachel??????

[Scene: Central Perk. Rachel is getting a cup of coffee as Joey and Phoebe enter and sit down.] Phoebe: Oh! Hey, Rach! Rachel: Hi! Hey, Happy Valentine's Day! Phoebe: Oh, you, too. Joey: Hey, so, uh, how's it going living over at Ross'? Rachel: It's good.

xiyangxia666hui的博客 3万+

保姆级教程:在RV1126开发板上跑通YOLOv5口罩检测(从数据集到摄像头实时识别)

本文提供了一份详细的RV1126开发板部署YOLOv5口罩检测系统的全流程指南,涵盖数据集准备、模型训练优化、边缘部署及实时摄像头识别等关键步骤。重点解析了RV1126开发板的性能特征、YOLOv5模型训练调优技巧、RKNN模型转换量化策略,以及部署过程中的性能优化方案,帮助开发者高效实现边缘端目标检测应用。

weixin_27310985的博客 108

RDMA性能调优实战:如何用ib_*命令榨干你的Mellanox网卡带宽?

本文深入探讨了如何利用ib_*命令工具集对RDMA网络进行深度性能调优,以充分释放Mellanox网卡的带宽潜力。文章从理解Send/Write/Read三种核心通信原语的差异入手,逐步指导读者通过调整消息大小、队列深度、内联数据等关键参数,并结合系统级的NUMA绑定、中断亲和性等优化策略,实现从基准测试到逼近硬件极限的性能提升。

weixin_29306011的博客 88

双向bfs --HDU - 3085 Nightmare Ⅱ

HDU - 3085 Nightmare Ⅱ:https://vjudge.net/problem/HDU-3085 题意: G和M在迷宫里寻找对方,M每秒可以走3步,G每次走一步,且只能上下左右走。迷宫里有鬼,鬼每次分裂到上下左右两格位置,直到占满所有格子,鬼可以穿墙,求G和M在不遇到鬼的情况下的最短相遇时间。 思路: 双向bfs,由于M每次走3步,所以M每次要处理3次,这要处理的点就是M每次要...

dajiangyou123456的博客 190

国产大模型 coding plan 实战决策指南:GLM、Kimi、豆包、abab 四大模型分层选型与工程落地

大模型辅助编程(coding assistant)正从概念验证走向规模化工程落地,其核心已不再是‘能否生成代码’,而是‘如何在真实开发流程中精准赋能’。理解模型能力边界、匹配技术栈特征、构建分层工作流(L1行级补全/L2函数生成/L3模块重构),是实现稳定提效的关键。GLM系列以工程稳定性见长,适合代码审计与测试生成;Kimi-Max凭借200K超长上下文胜任深度重构与合规审查;豆包响应快、框架适配深,是IDE高频补全的理想选择;abab系列则在IoT固件等跨模态场景中展现独特价值。本文聚焦国产模型在金融、

weixin_32238157的博客 427

HDU3085 - Nightmare Ⅱ(双向BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085题目大意:在一个N*M的网格里,有两个人 G 和 M,并且有两只鬼。 G每秒可以走一步,G每秒可以走三步,每只鬼可以分裂,分裂到周围的的两格。假设#为鬼分裂后为1,如下图所示。每只分裂后的新鬼可以继续分裂。

ACM_Fish的博客 451

VS code 调试显示preLaunchTask“g++“已终止,退出代码为-1。 生成‘... .exe‘文件错误

总结: 以后每个工程都需要按照这样的流程:创建一个.c/.cpp文件 -> F5运行 -> 配置tasks.json文件-> launch.json文件 -> 运行

qq_45151652的博客 2513

Qt + windows + Linux+QtInstallerFramework打包教程

本文介绍了使用Qt Installer Framework在Windows和Linux系统下打包Qt应用程序的完整教程。Windows部分详细说明了从下载安装框架、配置环境变量、生成release版本,到创建安装包目录结构和修改配置文件的完整流程。Linux部分则重点讲解了环境变量设置、桌面应用配置(.desktop文件)以及两种打包方案:一种使用linuxdeployqt生成AppImage文件,另一种采用类似Windows的安装包方式。教程提供了关键配置文件(config.xml、package.xml

Qt历险记,专注Qt技术,三年医疗设备研发经验,1年机器视觉研发经验,半年柔性传感研发经验。 516

HDU3085 NightmareⅡ 双向BFS

题目描述 给定一张N*M的地图,地图中有1个男孩,1个女孩和2个鬼。 字符“.”表示道路,字符“X”表示墙,字符“M”表示男孩的位置,字符“G”表示女孩的位置,字符“Z”表示鬼的位置。 男孩每秒可以移动3个单位距离,女孩每秒可以移动1个单位距离,男孩和女孩只能朝上下左右四个方向移动。 每个鬼占据的区域每秒可以向四周扩张2个单位距离,并且无视墙的阻挡,也就是在第k秒后所有与鬼的曼哈顿距离不超过2k的...

正月看雪花的博客 552

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

HDU 3085 —— Nightmare Ⅱ 双向BFS

原题:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题意: ' X ' 表示墙;' . ' 表示空地;' Z ' 表示鬼(鬼有两只); M每秒走三步;G每秒走一步;鬼每秒分裂,每次分裂距离鬼两步距离的地方都会被鬼覆盖,且鬼可以穿墙(当然鬼的分身也可分裂); 每秒鬼先分裂,然后M 和 G才走; 问在不遇到鬼的情况下,至少过几秒M和G可以相遇;

0x3f3f3f3f 480

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

HDU - 3085 Nightmare Ⅱ(双向BFS+曼哈顿距离)

HDU - 3085 Nightmare Ⅱ #include <cstring> #include <iostream> #include <algorithm> #include <queue> using namespace std; typedef pair<int, int> PII; const int N = 810; int n, m; char g[N][N]; int st[N][N]; PII ghost[2];

00后的博客 423

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

HDU3085 Nightmare Ⅱ[双向bfs]

D - Nightmare Ⅱ Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Last night, little erriyue had a horrible nightmare. He dreamed t

ControlBear的博客 1207

hdu3085 Nightmare Ⅱ 双向BFS 模板题

Nightmare ⅡTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3068    Accepted Submission(s): 874Problem DescriptionLast night, little erriyue had ...

popcjz的博客 2126

hdu 3085 Nightmare Ⅱ (双向bfs)

Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 716    Accepted Submission(s): 133 Problem Description Last night, litt

To be what you what to be! 1607

HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2790    Accepted Sub

A man can be destroyed, but not defeated. 1206

HDU 3085 Nightmare Ⅱ [双向BFS]

Nightmare Ⅱ Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts...

Janice_zh的博客 379

HDU - 3085 Nightmare Ⅱ 双向bfs

Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the p...

zamaochick的博客 206
上一篇: hdu1560DNA sequence(bfs)
下一篇: hdu1067Gap(bfs)
IDrandom
博客等级 码龄12年 31粉丝 125原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值