Safe Path

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

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.

 

题意:找起点S到终点F的最短路,怪物M在d步之内可以到达的点都是障碍。

思路:先把所有怪物加入队列同为起点,跑一遍bfs扩展d层停止,预处理出哪些点是障碍。然后跑一遍S到F的最短路。

这题有几个注意的地方:

1.预处理怪物如果用对于每个怪物分别dfs一次&&只用一种标记,那么wa,因为怪物a可能走到怪物b的附近把怪物b的路封掉,这样子的话,怪物b附近被封掉的点如果以b为起点,可以扩展更远,就被检查出已标记而不继续扩展了。这样会漏掉很多点。

2.如果用对于每个怪物分别dfs一次&&分别用不同标记,那么tle,正确性是没问题,但重复检查太多,超时。

3.如果用对于每个怪物分别bfs一次&&只用一种标记,那么wa,原因同第1点。

4.如果用对于每个怪物分别bfs一次&&分别用不同标记,那么tle,原因同第2点。

因此同时把所有怪物加入队列,跑一遍bfs。

存图用vector,判重用(第几行*列数+第几列)唯一编码。

bfs代码目前还不成风格,很丑而且容易丢东西出bug

#include<bits/stdc++.h>
using namespace std;

int n,m,d;
vector<int> a[200005];
map<int,bool> vis;
struct Node{
	int r,c,d;
}s,t;
queue<Node> Q;

void init()
{
	char str[200005];
	scanf("%d%d%d",&n,&m,&d);
	for(int i=0;i<n;i++)
	{
		scanf("%s",str);
		for(int j=0;j<m;j++)
		{
			if(str[j]=='M')a[i].push_back(1);
			else
			{
				a[i].push_back(0);	
				if(str[j]=='S')s=(Node){i,j,0};
				else if(str[j]=='F')t=(Node){i,j,0};
			}
		}
	}
}

bool inside(int x,int y){return x>=0&&x<n&&y>=0&&y<m;}

void monster()
{
	queue<Node> Q;
	map<int,bool> vis;
	for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(a[i][j])
	{
		vis[i*m+j]=1;
		Q.push((Node){i,j,0});
	}
	while(!Q.empty())
	{
		Node u=Q.front();Q.pop();
		Node v;
		v.r=u.r+1;v.c=u.c;v.d=u.d+1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&&v.d<=d){Q.push(v);a[v.r][v.c]=1;vis[v.r*m+v.c]=1;}
		v.r=u.r-1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&&v.d<=d){Q.push(v);a[v.r][v.c]=1;vis[v.r*m+v.c]=1;}
		v.r=u.r;v.c=u.c+1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&&v.d<=d){Q.push(v);a[v.r][v.c]=1;vis[v.r*m+v.c]=1;}
		v.c=u.c-1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&&v.d<=d){Q.push(v);a[v.r][v.c]=1;vis[v.r*m+v.c]=1;}
	}
}

int bfs()
{
	if(!a[s.r][s.c]){Q.push(s);vis[s.r*m+s.c]=1;}
	while(!Q.empty())
	{
		Node u=Q.front();Q.pop();
		if(u.r==t.r&&u.c==t.c)return u.d;
		Node v;
		v.r=u.r+1;v.c=u.c;v.d=u.d+1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&& 0==a[v.r][v.c]){Q.push(v);vis[v.r*m+v.c]=1;}
		v.r=u.r-1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&& 0==a[v.r][v.c]){Q.push(v);vis[v.r*m+v.c]=1;}
		v.r=u.r;v.c=u.c+1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&& 0==a[v.r][v.c]){Q.push(v);vis[v.r*m+v.c]=1;}
		v.c=u.c-1;
		if(inside(v.r,v.c)&& !vis[v.r*m+v.c]&& 0==a[v.r][v.c]){Q.push(v);vis[v.r*m+v.c]=1;}
	}
	return -1;
}

int main()
{
//	freopen("input.in","r",stdin);
	init();
	monster();
	cout<<bfs();
	return 0;
}

 

全部评论

相关推荐

原来已经一年了,因为没有加任何实验室没有学长学姐带,再一次偶然的机会下刷到我们学校的牛肉哥,和他聊天之后发现他也没加实验室能进大厂,我就燃起了希望,去年大概&nbsp;4&nbsp;月份找好路线&nbsp;零基础&nbsp;开始学&nbsp;5&nbsp;月背八股和开始刷算法很难受&nbsp;7-8&nbsp;月焦虑躯体化害怕找不到实习&nbsp;9&nbsp;月找到一家像样的小厂去实习了&nbsp;4&nbsp;个月大三上期末考试结束之后&nbsp;1&nbsp;月份回来边实习边准备工作压力很大&nbsp;当时只有字节、百度、商汤的面试,字节三面挂了,百度&nbsp;oc,商汤&nbsp;二面挂(差评&nbsp;无效面试),之后来深圳百度实习之后还是觉得不甘心一直没把算法和八股扔下一直在准备,百度实习的时候&nbsp;mt&nbsp;交给我一个特别重要的工作数据库迁移(特别感谢&nbsp;mt&nbsp;,这个需求学到了很多东西处理了一堆线上问题),本来看着暑期他们面试都很困难,然后听说百度要涨实习薪资(然而&nbsp;5&nbsp;月并没有涨),就想着留在百度吧也懒得面试了,4&nbsp;月&nbsp;20&nbsp;多的时候字节&nbsp;hr&nbsp;打电话约面问我要不要尝试一下询问了&nbsp;1&nbsp;月份三面为啥会挂有没有学习&nbsp;ai&nbsp;知识(因为字节这边后端岗位偏&nbsp;ai),我来到百度之后全面拥抱&nbsp;AI&nbsp;也认识了我的好兄弟&nbsp;X&nbsp;哥,他在百度&nbsp;XX&nbsp;部门&nbsp;Agent&nbsp;实习,他属于是我&nbsp;Agent&nbsp;的启蒙老师,来百度之后一直在了解&nbsp;AI&nbsp;这一块,我就接受了字节的面试,一面的时候&nbsp;20&nbsp;分钟实习拷打然后突然说&nbsp;30&nbsp;分钟代码考核我心就凉了以为是&nbsp;kpi,算法题是手撕高并发安全下的令牌桶限流器,我写了整整&nbsp;80&nbsp;多行代码最后也写出来了,但是从来没看到过出这种题能&nbsp;oc&nbsp;的我也就不管了,后边面试也是很顺利但是流程有点长可能一直在横向吧总结结果是好的!!!感谢这一年努力的自己和遇到的各位互联网大佬分享的知识!!!ps&nbsp;图二纯感慨&nbsp;(觉得🍬请不要喷我)欢迎大家一起交流学习呀!!!!
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
04-30 18:05
空屿编号:你把墨镜摘下来是不是这样😭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务