K - bfs

https://vjudge.net/contest/279018#problem/K

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

Examples

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

Note

Please note that monsters can run and kill you on start cell and on finish cell as well.

题目大意:

给k 即monster能走的步数,上下左右走。人不能受到monster攻击。

人从S出发到F,若安全到达,求最小步数。否则输出-1;

题目分析:

当初做这题,爆内存了。应该改用vector。再用一维数组代替二维数组标记monster能到达的地方。

先对monster dfs 标记危险区域。要特判一下人是否一开始就在monster攻击范围内。

在对人bfs();求出最小步数。

代码:

#include<cstdio>
#include<iostream>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2e5+10;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
vector<char> as[maxn];
int id[maxn],stx,sty,endx,endy,m,n,t,d;
struct T{int x,y;};
int flag=0;
bool check(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m)
		return true;
	return false;
}
void dfs(int x,int y,int d)
{
	if(d==0||flag==1)return;
	for(int i=0;i<4;i++)
	{
		int nx=x+dx[i];int ny=y+dy[i];
		if((nx==stx&&ny==sty)||(nx==endx&&ny==endy))
		{
			flag=1;
			return;
		}
		if(check(nx,ny))
		{
			if(as[nx][ny]=='M')
            {
                continue;
            }
			if( id[nx*m+ny]+1<id[x*m+y])
            {
                id[nx*m+ny]=d-1;
                dfs(nx,ny,d-1);
            }
		}
	}
}
int bfs()
{
	queue<T>q;
	T aa,bb;
	aa.x=stx,aa.y=sty;
	q.push(aa);
	id[stx*m+sty]=0;
	while(!q.empty())
	{
		aa=q.front();q.pop();
		for(int i=0; i<4; i++)
        {
            bb.x=dx[i]+aa.x;
            bb.y=dy[i]+aa.y;
            if(check(bb.x,bb.y))
            {
                if(id[bb.x*m+bb.y]==-1)
                {
                    id[bb.x*m+bb.y]=id[aa.x*m+aa.y]+1;
                    q.push(bb);
                }
            }
        }
	}
	return id[endx*m+endy];
}
int main()
{
	char s;
	while(~scanf("%d%d%d",&n,&m,&d))
	{
		for(int i=0;i<n;i++)
		{
			getchar();
			for(int j=0;j<m;j++)
			{
				s=getchar();
				if(s=='S')
				{
					stx=i,sty=j;
				}
				else if(s=='F')
				{
					endx=i,endy=j;
				}
				as[i].push_back(s);
				id[i*m+j]=-1;
			}
		}
		
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(as[i][j]=='M')
				{
					if(id[i*m+j]==d)
					continue;
					id[i*m+j]=d;
					dfs(i,j,d);
				}
			}
			if(flag==1)break;
		}
		if(flag==1)puts("-1");
		else
			printf("%d\n",bfs());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值