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

题意:从S走到F,M是怪兽,输入的d是怪兽的攻击范围。怪兽走d步能到达的地方不能走。
用bfs预处理M点。当遇到其他M点并且是第一次遇见时,让其他M点也进行更新。预处理完了之后就可以再一个bfs求最短路径了。一开始用的dfs预处理M点结果被卡在了48组。开数组保存的这个图,
例如3 4就是
1 2 3 4
5 6 7 8
9 10 11 12

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <ctime>
#include <stack>
using namespace std;
typedef long long ll;
int n,m,q,S,F,mp[200050],dd[200050],len[200050];
int cnt;
void bfs()
{
    memset(len,-1,sizeof(len));
    int l,r;
    l=r=0;
    if(mp[S]==0)
    {
        len[S]=0;
        dd[r]=S;
        r++;
    }
    while(l<r)
    {
        int xx=dd[l];
        l++;
        if(xx-m>0&&len[xx-m]==-1&&mp[xx-m]==0)///上
        {
            len[xx-m]=len[xx]+1,dd[r]=xx-m;
            if(xx-m==F)
                break;
            r++;
        }
        if(xx%m!=0&&len[xx+1]==-1&&mp[xx+1]==0)///右
        {
            len[xx+1]=len[xx]+1,dd[r]=xx+1,r++;
            if(xx+1==F)
                break;
        }
        if(xx+m<cnt&&len[xx+m]==-1&&mp[xx+m]==0)///下
        {
            len[xx+m]=len[xx]+1,dd[r]=xx+m,r++;
            if(xx+m==F)
                break;
        }
        if(xx%m!=1&&len[xx-1]==-1&&mp[xx-1]==0)///左
        {
            len[xx-1]=len[xx]+1,dd[r]=xx-1,r++;
            if(xx-1==F)
                break;
        }
    }
}
void bbfs(int x)
{
    int l,r;
    l=r=0;
    if(len[x]==-1)
    {
        dd[r++]=x;
        len[x]=0;
    }
    while(l<r)
    {
        int xx=dd[l];
        if(mp[xx]!=q)
            len[xx]=-1;
        l++;
        if(xx-m>0)///上
        {
            if(mp[xx-m]==q&&len[xx-m]==-1)
            {
                len[xx-m]=0;
                dd[r++]=xx-m;
            }
            else if(mp[xx]-1>mp[xx-m])
            {
                mp[xx-m]=mp[xx]-1;
                if(len[xx-m]==-1)
                    dd[r++]=xx-m,len[xx-m]=0;
            }
        }
        if(xx%m!=0)///右
        {
            if(mp[xx+1]==q&&len[xx+1]==-1)
            {
                len[xx+1]=0;
                dd[r++]=xx+1;
            }
            else if(mp[xx]-1>mp[xx+1])
            {
                mp[xx+1]=mp[xx]-1;
                if(len[xx+1]==-1)
                    dd[r++]=xx+1,len[xx+1]=0;
            }
        }
        if(xx+m<cnt)///下
        {
            if(mp[xx+m]==q&&len[xx+m]==-1)
            {
                len[xx+m]=0;
                dd[r++]=xx+m;
            }
            else if(mp[xx]-1>mp[xx+m])
            {
                mp[xx+m]=mp[xx]-1;
                if(len[xx+m]==-1)
                    dd[r++]=xx+m,len[xx+m]=0;
            }
        }
        if(xx%m!=1)///左
        {
            if(mp[xx-1]==q&&len[xx-1]==-1)
            {
                len[xx-1]=0;
                dd[r++]=xx-1;
            }
            else if(mp[xx]-1>mp[xx-1])
            {
                mp[xx-1]=mp[xx]-1;
                if(len[xx-1]==-1)
                    dd[r++]=xx-1,len[xx-1]=0;
            }
        }
    }
}
void init()///预处理
{
    memset(len,-1,sizeof(len));
    for(int i=1; i<cnt; i++)
    {
        if(mp[i]==q+1)
        {
            bbfs(i);
        }
    }
}
void display()
{
    for(int i=0; i<n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            printf("%4d ",mp[i*m+j]);
        }
        printf("\n");
    }
}
int main()
{
    char str[200050];
    scanf("%d%d%d",&n,&m,&q);
    cnt=1;
    for(int i=0; i<n; i++)
    {
        scanf("%s",str+1);
        for(int j=1; j<=m; j++)
        {
            if(str[j]=='.')
            {
                mp[cnt++]=0;
            }
            else if(str[j]=='M')
            {
                mp[cnt++]=q+1;
            }
            else if(str[j]=='S')
            {
                S=cnt;
                mp[cnt++]=0;
            }
            else if(str[j]=='F')
            {
                F=cnt;
                mp[cnt++]=0;
            }
        }
    }
    init();
   /// display();
    ///display();
    bfs();
    printf("%d\n",len[F]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值