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.


這道題其實數據還是有點水,用vector居然也能過,下次一定轉換爲一維做。

題意就是n*m的方格里面能不能到達終點,有意思的是怪獸有攻擊方位,先DFS標記處怪獸的攻擊範圍,然後BFS最短路就解決。

關鍵點是DFS注意優化不然20組45組數據要超時。


#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章