码迷,mamicode.com
首页 > 其他好文 > 详细

G - BFS Gym - 101755H

时间:2020-01-13 01:04:29      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:imm   lines   queue   ima   就是   har   inpu   pac   struct   

题目:G - BFS

Gym - 101755H

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

 

题目描述:

先输入3个数n,m,d。分别表示迷宫行,列和怪兽能走的步数。

就是走迷宫,从S出发走到F。求最短路径。(如果走进怪兽的范围,就会被杀死)

分析:

走迷宫求最短距离,一般都是用广度搜索,用结构体来记录走过的步数,比如开始地点S的距离为0,每走一步加1。一般走到F就是最短的路径。

由于有怪物的存在,我们需要先把它的攻击范围标记出来,我们广搜的时候要避开。(坑点:如果是以一个怪兽一个怪兽来标记攻击范围的话,就会超时。这时需要用深搜或广搜,来跳过已经标记过的地点。)

最后由于地图大小为2<n*m<200000;开二维数组的话会超内存,所以开一个一维大小为200000以上的数组。只需要行数乘以行大小加上列数,就可以向二维数组一样使用。

代码:

#include<stdio.h>
#include<math.h>
#include<iostream> 
#include<queue>
#define MIN(x,y) x<y?x:y;
using namespace std;
int n,m,d;
int over;

struct sta
{
    int y,x;
    char c;
    char d;
    int data;
}a[200007];

queue<sta> P;
int dy[4]={1,0,-1,0};
int dx[4]={0,1,0,-1};

int mos()
{
    while(!P.empty())
    {
        struct sta M=P.front();
        M.d=y;
        P.pop();
        if(M.data<d)
        {
            for(int i=0;i<4;i++)
            {
                int nx=M.x+dx[i],ny=M.y+dy[i];
                if(nx<0||nx>=m||ny<0||ny>=n)
                continue;
                if(a[ny*m+nx].c==S)
                {
                    over=1;
                    return 0;
                }
                if(a[ny*m+nx].d==n)
                {
                    a[ny*m+nx].c=M;
                    a[ny*m+nx].d=y;
                    a[ny*m+nx].data=M.data+1;
                    P.push(a[ny*m+nx]);
                }
            }
         } 
    }
    return 0;
}


int bfs(int y,int x)
{
    queue<sta> q;
    a[y*m+x].data=0;
    q.push(a[y*m+x]);
    int min=2000191;
    while(!q.empty())
    {
        struct sta s=q.front();
        q.front().d=y;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int nx=s.x+dx[i],ny=s.y+dy[i];
            if(nx<0||nx>=m||ny<0||ny>=n) continue;
            if(a[ny*m+nx].c==F)
            {
                min=MIN(min,a[s.y*m+s.x].data+1);
                return min;    
            }
            else if(a[ny*m+nx].c==.&&a[ny*m+nx].d==n)
            {
                a[ny*m+nx].data=s.data+1;
                a[ny*m+nx].d=y;
                q.push(a[ny*m+nx]);
            }
        }
    }
    if(q.empty()) over=1;
    return min;
}
int main()
{
    int p,q;
    scanf("%d %d %d",&n,&m,&d);
    getchar();
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            a[i*m+j].y=i;
            a[i*m+j].x=j;
            a[i*m+j].c=getchar();
            a[i*m+j].d=n;
        }
        getchar();
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i*m+j].c==M)
            {
                a[i*m+j].d=n;
                a[i*m+j].data=0;
                P.push(a[i*m+j]);
            }
            if(a[i*m+j].c==S)
            {
                p=i;
                q=j;
            }
        }
    }
    mos();
    int res=bfs(p,q);
    if(over)
    {
        cout<<-1<<\n;
    }
    else
    {
        
        cout<<res<<\n;
    }
    return 0;
}

G - BFS Gym - 101755H

标签:imm   lines   queue   ima   就是   har   inpu   pac   struct   

原文地址:https://www.cnblogs.com/studyshare777/p/12185421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!