2019_GDUT_ newborn Topics I anthology G Gym - 101755H

topic:

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

Analysis: BFS, BFS first place on the monster, the monster d steps within a map marked as not all go, and then to the starting point of BFS. Beginning time may have been in the range of monster output directly -1. Bfs time for the monster to be careful not to cover off another monster, a monster must pay attention to a situation that may be trapped in another monster, open two queues, are stored in advance information for each monster in there, do a monster range dfs, another save by the corresponding point from which to expand the monster, you do not need more than d-step search again.

Code:

#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;
}
Published 14 original articles · won praise 0 · Views 303

Guess you like

Origin blog.csdn.net/qq_39581539/article/details/103964429