当前位置: 首页 > news >正文

【多重BFS】Monsters

题目描述

You and some monsters are in a labyrinth. When taking a step to some direction in the labyrinth, each monster may simultaneously take one as well. Your goal is to reach one of the boundary squares without ever sharing a square with a monster.
Your task is to find out if your goal is possible, and if it is, print a path that you can follow. Your plan has to work in any situation; even if the monsters know your path beforehand.

输入

The first input line has two integers n and m: the height and width of the map.
After this there are n lines of m characters describing the map. Each character is . (floor), # (wall), A (start), or M (monster). There is exactly one A in the input.
Constraints
1 ≤ n,m ≤ 1000

输出

First print "YES" if your goal is possible, and "NO" otherwise.
If your goal is possible, also print an example of a valid path (the length of the path and its description using characters D, U, L, and R). You can print any path, as long as its length is at most n * m steps.

样例输入

复制

5 8
########
#M..A..#
#.#.M#.#
#M#..#..
#.######
样例输出

复制

题目翻译

你和一些怪物在一个迷宫里。当你向迷宫中的某个方向迈出一步时,每个怪物可能也会同时迈出一步。你的目标是到达迷宫的某个边界格子,且过程中永远不会与怪物处于同一个格子。

你的任务是判断这个目标是否可行,如果可行,输出一条你可以遵循的路径。你的计划必须在任何情况下都有效,即使怪物事先知道你的路径。

输入

第一行输入两个整数 n 和 m:分别表示地图的高度和宽度。
接下来 n 行,每行有 m 个字符,描述地图:

  • . 表示地板(可以通行)
  • # 表示墙壁(不可通行)
  • A 表示起点(你的初始位置)
  • M 表示怪物的初始位置

输入中恰好有一个 A

约束

1 ≤ n, m ≤ 1000

输出

如果目标可行,首先输出 "YES",然后输出路径的长度和路径描述(使用字符 D、U、L、R 分别表示下、上、左、右)。
如果目标不可行,输出 "NO"。

你可以输出任何一条有效的路径,只要其长度不超过 n * m 步。

  1. 采用双 BFS 策略:

    • 先对所有怪物进行多源 BFS,计算每个位置最早被怪物到达的时间
    • 再对玩家进行 BFS,确保玩家到达每个位置的时间严格小于怪物到达时间
  2. 修复核心逻辑错误:

    • 原代码将玩家和怪物放在同一队列,导致移动不同步
    • 新代码通过两个独立 BFS,先计算怪物到达时间,再规划玩家路径
  3. 增加距离数组:

    • player_dist 记录玩家到达每个位置的步数
    • monster_dist 记录怪物到达每个位置的步数
    • 确保玩家到达某位置的步数 + 1 < 怪物到达该位置的步数

代码

#include<bits/stdc++.h>
using namespace std;int lab[1001][1001];
int n,m;
queue<pair<int,int>>pla,mon;
string s[1001];
char path[1001][1001];int check(int x,int y)
{return x>=0&&x<n&&y>=0&&y<m&&!lab[x][y];
}int main()
{cin>>n>>m;for(int i=0;i<n;++i)cin>>s[i];for(int i=0;i<n;++i)for(int j=0;j<m;++j)path[i][j]='#';vector<vector<int>>dis_p(1001,vector<int>(1001,INT_MAX));vector<vector<int>>dis_m(1001,vector<int>(1001,INT_MAX));for(int i=0;i<n;++i){for(int j=0;j<m;++j){lab[i][j]=(s[i][j]=='#');if(s[i][j]=='A'){pla.push({i,j}),dis_p[i][j]=0;path[i][j]='S';}if(s[i][j]=='M'){mon.push({i,j}),dis_m[i][j]=0;}}}int dx[]{-1,0,1,0},dy[]{0,1,0,-1};char dd[]{'U','R','D','L'};	vector<char>res;while(!mon.empty()){int x=mon.front().first;int y=mon.front().second;mon.pop();for(int i=0;i<4;++i){int nx=x+dx[i];int ny=y+dy[i];if(check(nx,ny)&&dis_m[nx][ny]==INT_MAX){dis_m[nx][ny]=dis_m[x][y]+1;mon.push({nx,ny});}}}int f=0;//如果本来start就在边界 res是空的while(!pla.empty()){int x=pla.front().first;int y=pla.front().second;pla.pop();if(x==0||x==n-1||y==0||y==m-1){f=1;int nx=x,ny=y;while(path[nx][ny]!='S'){char c=path[nx][ny];res.push_back(c);if(c=='U')nx++;else if(c=='R')ny--;else if(c=='D')nx--;else ny++;}break;}for(int i=0;i<4;++i){int nx=x+dx[i];int ny=y+dy[i];if(check(nx,ny)&&dis_p[nx][ny]==INT_MAX&&dis_p[x][y]+1<dis_m[nx][ny]){dis_p[nx][ny]=dis_p[x][y]+1;pla.push({nx,ny});path[nx][ny]=dd[i];}}}if(!f){cout<<"NO";return 0;}cout<<"YES\n"<<res.size()<<'\n';if(!res.empty())reverse(res.begin(),res.end());for(auto i:res)cout<<i;return 0;	
}

http://www.dtcms.com/a/319233.html

相关文章:

  • 调用阿里云-阿里云百炼 AI
  • 表驱动法-灵活编程范式
  • Java 中 Object 类的解析:知识点与注意事项
  • Oracle参数Process
  • 深度学习的视觉惯性里程计(VIO)算法优化实践
  • PCB制造中压接孔、插接孔、沉头孔、台阶孔的区别及生产流程
  • [Oracle] MOD()函数
  • 数据库入门:从零开始构建你的第一个数据库
  • idea工具maven下载报错:PKIX path building failed,配置忽略SSL检查
  • [Oracle] CEIL()函数
  • 无人机航拍数据集|第7期 无人机绵羊红外目标检测YOLO数据集1964张yolov11/yolov8/yolov5可训练
  • 计算虚拟化技术
  • vscode.window.activeTextEditor 获取不到 png 图片路径问题
  • 僵尸进程问题排查
  • Github创建仓库,克隆提交代码到远程
  • 内存泄漏系列专题分析之三十二:高通相机CamX ION/dmabuf内存管理机制CmdBuffer
  • 【3D图像技术分析与实现】谷歌的AlphaEarth是如何实现的?
  • 鸿蒙RichEditor
  • 使用萤石云播放视频及主题模版配置
  • python安装部署rknn-toolkit2(ModuleNotFoundError: No module named ‘rknn_toolkit2‘)
  • 技术速递|Copilot Coding Agent:自定义设置步骤更可靠、更易于调试
  • P8250 交友问题
  • 表单元素与美化技巧:打造用户友好的交互体验
  • zookeeper因jute.maxbuffer启动异常问题排查处理
  • 如何开发一个运行在windows系统服务器上的服务
  • “物联网+职业本科”:VR虚拟仿真实训室的发展前景
  • 纳米陶瓷与光子集成:猎板PCB定义下一代VR硬件的技术蓝图
  • 【unity实战】使用Unity程序化生成3D随机地牢(附项目源码)
  • 飞机起落架轮轴深孔中间段电解扩孔内轮廓测量 - 激光频率梳 3D 轮廓检测
  • 如何将Dubbo从Zookeeper平滑地迁移到Nacos?