2019_GDUT_新生专题I选集 G Gym - 101755H

题目:

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.

Input
The first line contains three non-negative integers n, m and d (2 ≤ n·m ≤ 200000, 0 ≤ d ≤ 200000) — the size of the map and the maximal distance at which monsters are dangerous.

Each of the next n lines contains m characters. These characters can be equal to «.», «M», «S» and «F», which denote empty cell, cell with monster, start cell and finish cell, correspondingly. Start and finish cells are empty and are presented in the input exactly once.

Output
If it is possible to get alive from start cell to finish cell,output minimal number of steps required to do it. Otherwise, output
«-1».
Input
5 7 1
S.M…M


M…M…
…F
Output
12
Input
7 6 2
S…
…M…

…M

M…
…F
Output
11
Input
7 6 2
S…
…M…


…M
M…
…F
Output
-1
Input
4 4 2
M…
.S…

…F
Output
-1

分析:BFS,先对怪兽的地方BFS,将怪兽d步以内的地图全部标记为不可走,然后再对起点BFS。可能一开始的时候就已经在怪兽的范围内,直接输出-1。对怪兽bfs的时候要注意不能盖掉别的怪兽,还要注意某一个怪兽可能被别的怪兽困住的情况,开两个队列,均提前保存好每个怪兽的信息在里面,一个做怪兽范围的dfs,另一个保存对应点是由哪个怪兽拓展而来,超过d步则不需再搜索。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue> 
struct node
{
	int x,y;
};
using namespace std;
int M[2000*2000],D,n,m,xs,ys,xf,yf,ans=2000000000;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
queue <node> q,p;
void bfs1()
{
	int (*a)[m]=(decltype(a))M;
	int x,y;
	while (!p.empty())
	{
		node f = p.front();
		node pre=q.front();
		for (int i=0;i<4;i++)
		{
			x=f.x+dx[i];
			y=f.y+dy[i];
			if (abs(x-pre.x)+abs(y-pre.y)<=D && a[x][y]==0 && x>0 && y>0 && x<=n && y<=m)
			{
				p.push(node{x,y});
				a[x][y]=-2;
				q.push(pre);
			}
		}
		p.pop();
		q.pop();
	}
}
void bfs(int x1,int y1)
{
	int (*a)[m]=(decltype(a))M;
	int x,y;
	p.push(node{x1,y1});
	a[x1][y1]=1;
	while (!p.empty())
	{
		for (int i=0;i<4;i++)
		{
			x=p.front().x+dx[i];
			y=p.front().y+dy[i];
			if (a[x][y]==0 && x>0 && y>0 && x<=n && y<=m)
			{
				if (x==xf && y==yf)
				{
					a[x][y]=a[p.front().x][p.front().y]+1;
					if (ans>a[x][y])
					ans=a[x][y]-1;
					return;
				}
				p.push(node{x,y});
				a[x][y]=a[p.front().x][p.front().y]+1;
			}
		}
		p.pop();
	}
}
int main()
{
	cin>>n>>m>>D;
	int (*a)[m]=(decltype(a))M;
	char s[300000];
	for (int i=1;i<=n;i++)
	{
		cin>>s;
		for (int j=0;j<m;j++)
		{
			if (s[j]=='M')
			{
				a[i][j+1]=-1;
				q.push(node{i,j+1});
				p.push(node{i,j+1});
			}
			if (s[j]=='S') xs=i,ys=j+1;
			if (s[j]=='F') xf=i,yf=j+1;
		}
	}
	bfs1();
	if (a[xs][ys]==0)
	bfs(xs,ys);
	if (a[xf][yf]>0) cout<<ans;
	else cout<<-1;
}
发布了14 篇原创文章 · 获赞 0 · 访问量 303

猜你喜欢

转载自blog.csdn.net/qq_39581539/article/details/103964429
今日推荐