1. 程式人生 > >H - Safe Path Gym - 101755H 國外區域賽

H - Safe Path 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 nm

 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».

水題

一波搜出怪物到達各點的時間 一波搜出人是否能到達終點

vector存 resize拉空間 節省記憶體和時間開銷

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
vector<int> moster[maxn];
vector<int> dis[maxn];
vector<char> s[maxn];
int sx,sy,ex,ey,n,m,d;
int dir[4][2]={-1,0,0,-1,1,0,0,1};

struct node
{
	int x,y;
};
queue<node> que;

int bfs()
{
	while(!que.empty()) que.pop();
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			dis[i][j]=inf;
		}
	}
	dis[sx][sy]=0;	
	node temp;
	temp.x=sx,temp.y=sy;
	if(moster[sx][sy]>d) que.push(temp);
	while(!que.empty())
	{
		node in,ne;
		in=que.front();
		que.pop();
		if(in.x==ex&&in.y==ey)
		break;
		for(int i=0;i<4;i++)
		{
			ne.x=in.x+dir[i][0];
			ne.y=in.y+dir[i][1];
			if(ne.x<0||ne.x>=n||ne.y<0||ne.y>=m||dis[ne.x][ne.y]!=inf||moster[ne.x][ne.y]<=d)
			continue;
			else
			{
				dis[ne.x][ne.y]=dis[in.x][in.y]+1;
				que.push(ne);
			}
		}
	}
	/*
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			printf("%1d    ",dis[i][j]);
		}
		printf("\n");
	}
	*/
	return dis[ex][ey];
}

int main()
{
	scanf("%d%d%d",&n,&m,&d);
	for(int i=0;i<n;i++)
	{
		dis[i].resize(m);
		moster[i].resize(m);
		s[i].resize(m);
	}
	char str[maxn];
	for(int i=0;i<n;i++)
	{
		scanf("%s",str);
		for(int j=0;j<m;j++)
		{
			s[i][j]=str[j];
			if(str[j]=='S') {sx=i,sy=j;}
			if(str[j]=='F') {ex=i,ey=j;}		
		}
	}
	//printf("%d		%d\n",ex,ey);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			moster[i][j]=inf;
			if(s[i][j]=='M')
			{ 
				moster[i][j]=0;
				node temp;
				temp.x=i,temp.y=j;
				que.push(temp);
			}
		}
	}
	while(!que.empty())
	{
		node in,ne;
		in=que.front();
		que.pop();
		for(int i=0;i<4;i++)
		{
			ne.x=in.x+dir[i][0];
			ne.y=in.y+dir[i][1];
			if(ne.x<0||ne.x>=n||ne.y<0||ne.y>=m||moster[ne.x][ne.y]!=inf)
			continue;
			else
			{
				moster[ne.x][ne.y]=moster[in.x][in.y]+1;
				que.push(ne);
			}
		}
	}
	/*
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			printf("%1d    ",moster[i][j]);
		}
		printf("\n");
	}*/
	int ans=bfs();
	if(ans==inf)printf("-1\n");
	else printf("%d\n",ans);
}