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

【多重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.xdnf.cn/news/17320.html

相关文章:

  • 人工智能——自动微分
  • Spring Boot + ONNXRuntime CPU推理加速终极优化
  • 02电气设计-安全继电器电路设计(让电路等级达到P4的安全等级)
  • PostgreSQL面试题及详细答案120道(61-80)
  • 仁懋电子MOT11N45——音响电路的卓越选择
  • 亚马逊广告运营:有什么好用的辅助工具
  • 接口自动化-pytest
  • 如何实现冷库的远程监控?冷库远程监控物联网解决方案
  • Flink-1.19.0-核心源码详解
  • 汽车娱乐信息系统域控制器的网络安全开发方案
  • Redis为什么要引入多线程?
  • 齐护机器人小智AI_MCP图形化编程控制Arduino_ESP32
  • 2025 年最佳no-code和open-source AI Agents
  • GitCode 7月:小程序积分商城更名成长中心、「探索智能仓颉!Cangjie Magic 体验有奖征文活动」圆满收官、深度对话栏目持续热播
  • 数据结构:双向链表(Doubly Linked List)
  • 谷歌DeepMind发布的全新世界模型“Genie 3”
  • 远程连接----ubuntu ,rocky 等Linux系统,WindTerm_2.7.0
  • vue3 el-select el-option 使用
  • 每日算法刷题Day57:8.6:leetcode 单调栈6道题,用时2h
  • 算法训练营DAY55 第十一章:图论part05
  • 【2025.08.06最新版】Android Studio下载、安装及配置记录(自动下载sdk)
  • 达梦数据库数据守护集群启动与关闭标准流程
  • hive专题面试总结2
  • [AI]从零开始的SDXL LORA训练教程
  • 微信小程序中使用TensorFlowJS从环境搭建到模型训练及推理模型得到预测结果
  • OpenAI最新开源:GPT-OSS原理与实践
  • 学习bug
  • 力扣热题100------136.只出现一次的数字
  • 机器学习之朴素贝叶斯
  • Unix/Linux 系统编程中用于管理信号处理行为的核心概念或模型