# Gym - 101755H Safe Path

H. Safe Path
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

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
Copy
5 7 1
S.M...M
.......
.......
M...M..
......F

output
Copy
12

input
Copy
7 6 2
S.....
...M..
......
.....M
......
M.....
.....F

output
Copy
11

input
Copy
7 6 2
S.....
...M..
......
......
.....M
M.....
.....F

output
Copy
-1

input
Copy
4 4 2
M...
.S..
....
...F

output
Copy
-1

Note

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

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<string.h>
#include <vector>
#include<queue>
const int maxn=200005;
using namespace std;
vector<char>mat[maxn];
int v[maxn];
int sx,sy,ex,ey;
int m,n,t;
int dirx[]= {-1,1,0,0};
int diry[]= {0,0,1,-1};
struct node
{
int x,y;
} no,ne;
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 fx=dirx[i]+x;
int fy=diry[i]+y;
if((fx==ex&&fy==ey)||(fx==sx&&fy==sy))
{
flag=1;
return ;//如果起点或者终点在攻击范围直接结束程序
}
if(check(fx,fy))
{
if(mat[fx][fy]=='M')
{
continue;
}

if( v[fx*m+fy]+1<v[x*m+y])//如果即将搜索的点的扩散范围大于这次搜索过来的范围就不管，减少时间复杂度。
{
v[fx*m+fy]=d-1;//如果这个点的当前扩散范围小于即将更新过来的范围就更新。注意每个点不只是搜索一次
DFS(fx,fy,d-1);
}

}
}
}
int BFS()
{
queue<node>q;
no.x=sx,no.y=sy;
q.push(no);
v[sx*m+sy]=0;
while(!q.empty())
{
no=q.front();
q.pop();
for(int i=0; i<4; i++)
{
ne.x=dirx[i]+no.x;
ne.y=diry[i]+no.y;
if(check(ne.x,ne.y))
{
if(v[ne.x*m+ne.y]==-1)
{
v[ne.x*m+ne.y]=v[no.x*m+no.y]+1;
q.push(ne);
}
}
}
}
return v[ex*m+ey];
}
int main()
{
int d;
char s;
scanf("%d %d %d",&n,&m,&d);
t=d;
for(int i=0; i<n; i++)
{
getchar();
for(int j=0; j<m; j++)
{
scanf("%c",&s);
if(s=='S')
{
sx=i,sy=j;
}
if(s=='F')
ex=i,ey=j;
mat[i].push_back(s);
v[i*m+j]=-1;
}
}

for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(mat[i][j]=='M')
{
if(v[i*m+j]==d)
continue;
v[i*m+j]=d;
DFS(i,j,d);
}
}
if(flag==1)
break;
}

if(flag==1)
cout<<-1<<endl;
else
{
int ans = BFS();
cout<<ans<<endl;
}

return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120