当前位置: 首页 > 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.lryc.cn/news/613469.html

相关文章:

  • 【实时Linux实战系列】基于实时Linux的高频交易系统构建
  • 【C语言】深入理解编译与链接过程
  • 数据标注之数据集的类型与如何标注
  • 时间并非维度:论其作为空间变化的转换系数
  • 大模型LL04 微调prompt-Tuning方法入门(背景与发展)
  • 深度学习的视觉惯性里程计(VIO)算法优化实践
  • 数据结构学习之二叉树
  • 深度学习(2):自动微分
  • LSTM 单变量时序预测—pytorch
  • JAVA第六学:数组的使用
  • 【数据结构】二叉树练习
  • S7-1200 串行通信介绍
  • 一场 Dark Theme A/B 测试的复盘与提效实践
  • Linux上MySql CPU 占用异常
  • SpringBoot中的单例注入方式
  • windows有一个企业微信安装包,脚本执行并安装到d盘。
  • VSCode ssh一直在Setting up SSH Host xxx: Copying VS Code Server to host with scp等待
  • 开发避坑指南(20) :MyBatis操作Oracle插入NULL值异常“无效列类型1111“解决方案
  • DrissionPage实战案例:小红书旅游数据爬取
  • TDengine IDMP 文档介绍
  • 腾讯位置服务 —— 预估订单路线金额(使用Drools规则引擎处理)
  • 机器学习在量化中的应用:如何从逻辑回归到XGBoost实现高效预测?
  • [Oracle] DECODE()函数
  • DBeaver 25.1.0 转储数据库失败解决方案(适配最新版界面)
  • [Oracle] GREATEST()函数
  • 数据库入门:从零开始构建你的第一个数据库
  • 一个基于固定 IP地址查询天气的 C 语言程序,通过调用第三方天气 API:
  • Oracle 关闭 impdp任务
  • Oracle 12c + Pl/Sql windows系统下表空间创建、迁移,dmp备份导入,数据库字符集更改
  • 图论(1):图数据结构