Safe Path/GYM 101755H/BFS

Safe Path/GYM 101755H/BFS

题目

You play a new RPG. The world map in it is represented by a grid of n × m cells. Any playing character staying in some cell can move from this cell in four directions — to the cells to the left, right, forward and back, but not leaving the world map.
Monsters live in some cells. If at some moment of time you are in the cell which is reachable by some monster in d steps or less, he immediately runs to you and kills you.
You have to get alive from one cell of game field to another. Determine whether it is possible and if yes, find the minimal number of steps required to do it.

题目来源:GYM 101755H
题目链接

题意

有一个n*m的迷宫,先需要从S点走到F点,迷宫中有小怪,当有人走到距离它d格内时小怪会发起攻击,求S到F的最小步数,如果无法到达输出-1。

解法

先用BFS对小怪能攻击的格标记,然后再跑一遍BFS求最小步数。需要注意题目只给出n*m<=200000,所以需要将二维图转化为一维的。

代码:

#include <cstring>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m,ans,d,sx,sy,ex,ey;
bool f[200010],flag;
char st[200010];
queue <int> X,Y,T,qx,qy;
void bfs1() 
{ 
	int x,y,t,nx,ny;
	while(!qx.empty()) 
	{
		x=qx.front();y=qy.front();t=T.front();
		qx.pop();qy.pop();T.pop();
		for(int i=0;i<4;i++) 
		{
			nx=x+dx[i];
			ny=y+dy[i];
			if (nx<1 || nx>n || ny<1 || ny>m ||f[(nx-1)*m+ny] || t>=d) continue;
			qx.push(nx);
			qy.push(ny);
			T.push(t+1);
			f[(nx-1)*m+ny]=true;
		}
	}
}
void bfs(int sx,int sy) 
{ 
	X.push(sx);
	Y.push(sy);
	while (!T.empty()) T.pop();
	T.push(0);
	int x,y,t,nx,ny;
	if (f[(sx-1)*m+sy]) return;
	f[(sx-1)*m+sy]=true;
	while(!X.empty()) 
	{
		x=X.front();y=Y.front();t=T.front();
		X.pop();Y.pop();T.pop();
		for(int i=0;i<4;i++) 
		{
			nx=x+dx[i];
			ny=y+dy[i];
			if (nx<1 || nx>n || ny<1 || ny>m || f[(nx-1)*m+ny]) continue;
			X.push(nx);
			Y.push(ny);
			T.push(t+1);
			f[(nx-1)*m+ny]=true;
			if(nx==ex && ny==ey) ans=t+1,flag=true;
		}
	}
}
int main()
{
	scanf("%d%d%d",&n,&m,&d);
	for (int i=1;i<=n;i++) 
	{
		scanf("%s",st);
		for (int j=0;j<m;j++) 
		{
			if (st[j]=='M') 
			{
				qx.push(i);qy.push(j+1);f[(i-1)*m+j+1]=true;T.push(0);
			}
			if (st[j]=='S') 
			{
				sx=i;sy=j+1;
			}
			if (st[j]=='F') 
			{
				ex=i;ey=j+1;
			}
		}
	}
	bfs1();
	flag=false;
	bfs(sx,sy);
	if (flag) printf("%d",ans); else printf("-1");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值