# 搜索加标记

Gym - 101755H

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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int Maxn = 1e6 + 100;
int n,m,t;
int a[Maxn],d;
char str[Maxn];
bool vis[Maxn];
int dis[Maxn];
int dd[Maxn];
int cnt,enx,eny,stx,sty;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,-1,1};
struct node {
int x,y;
} g[Maxn];
bool check(int a,int b) {
if(a >= 1 && b >= 1 && a <= n && b <= m && dd[a * m + b] == 0)
return true;
return false;
}
void bfss() {
queue<node>q;
for(int i = 0; i < cnt; i++)
q.push(g[i]);
while(!q.empty()) {
node b = q.front();
q.pop();
if(dd[b.x * m + b.y] == 0)
continue;
for(int i=0; i < 4; i++) {
int X = b.x + xx[i];
int Y = b.y + yy[i];
if(check(X,Y)) {
node c;
c.x = X;
c.y = Y;
dd[X * m + Y] = dd[b.x * m + b.y] - 1;
if(dd[X * m + Y] != 0)
q.push(c);
}
}
}
return ;
}

void bfs() {
queue<node>q;
node a,b,c;
a.x = stx;
a.y = sty;
vis[stx * m + sty] = 1;
q.push(a);
while(!q.empty()) {
b = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
int X = b.x + xx[i];
int Y = b.y + yy[i];
if(check(X,Y) && !vis[X * m + Y]) {
dis[X * m + Y] = dis[b.x * m + b.y] + 1;
vis[X * m + Y] = 1;
c.x = X;
c.y = Y;
q.push(c);
}
}

}
}
int main() {
while(~scanf("%d%d%d",&n,&m,&d)) {
getchar();
for(int i = 1; i <= n; i++)
scanf("%s",&str[i * m + 1]);
memset(vis,0,sizeof(vis));
memset(dd,0,sizeof(dd));
memset(dis,0,sizeof(dis));
for(int i = 1; i <= n; i++)  {
for(int j = 1; j <= m; j++) {
if(str[i * m + j] == 'S')
stx = i,sty = j;
else if(str[i * m + j] == 'F')
enx = i,eny = j;
else if(str[i * m + j] == 'M') {
node a;
dd[i * m + j] = d + 1;
a.x = i;
a.y = j;
g[cnt++] = a;
}
}
}
bfss();
if(dd[stx * m + sty] > 0 || dd[enx * m + eny] > 0) {
printf("-1\n");
continue;
}
bfs();
if(dis[enx * m + eny] == 0) {
printf("-1\n");
} else printf("%d\n",dis[enx * m + eny]);
}
return 0;
}



• 本文已收录于以下专栏：

举报原因： 您举报文章：搜索加标记 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)