G - BFS Gym - 101755H

题目: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;
}

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

时间: 2024-10-11 03:47:22

G - BFS Gym - 101755H的相关文章

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)G——BFS——Cutting Figure

Description You've gotten an n × m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make i

图的创建和遍历(BFS/DFS)

图的表示方法主要有邻接矩阵和邻接表.其中邻接表最为常用,因此这里便以邻接表为例介绍一下图的创建及遍历方法. 创建图用到的结构有两种:顶点及弧 struct ArcNode { int vertexIndex; //该弧指向的顶点位置 struct ArcNode* next; //指向下一个弧 InfoType info; //该弧的相关信息,如权重等 }; struct Vertex { VertexType data; //顶点信息 ArcNode* firstArc; //指向第一条依附该

USACO maze1 BFS

好久不写bfs了,中间失误了好长时间,在扩展新节点的时候一旦扩展成功就应该标记节点为已访问,不然有些情况下会无限扩展队列.题目的输入处理略坑爹,可以先当字符串读进来,然后用一个数组标记每个节点四周墙的情况,最后从两个出口搜索一遍,用流水填充,最后取两遍的较小值,然后取整个地图的最大值作为结果 /* ID:kevin_s1 PROG:maze1 LANG:C++ */ #include <iostream> #include <cstdio> #include <string&

循环节(BFS)

循环节 时间限制: 1 Sec  内存限制: 64 MB提交: 56  解决: 16[提交][状态][讨论版] 题目描述 第一节是英语课.今天,老师又教了桐桐很多单词.桐桐发现所有单词都有循环节(大写字母一律化成小写字母).如a(循环长度为1,循环节为 a),luLul(循环长度为2,循环节为lu),OlyMPic(循环长度为7,循环节为olympic),CcCcccc(循环长度为1,循环节为 c),等等. 而且,桐桐发现这能加速她背单词.她上课和ROBIN说了话,老师罚她背单词.虽然就一个,但

算法系列之图--BFS

广度优先搜索以源结点s为出发点,算法始终将已发现和未发现结点之间的边界,沿其广度方向向外扩展.也即算法需要在发现所有距离源结点s为k的所有结点之后才会去发现距离源结点距离为k+1的其他结点. talk is cheap,show me the code!上具体的代码,最容易解释这一切. 该例子是以无向图为基础,代码中注释有具体说明,不在解释每一步: 1 #include <iostream> 2 #include <list> 3 #include <queue> 4

数据结构基础(21) --DFS与BFS

DFS 从图中某个顶点V0 出发,访问此顶点,然后依次从V0的各个未被访问的邻接点出发深度优先搜索遍历图,直至图中所有和V0有路径相通的顶点都被访问到(使用堆栈). //使用邻接矩阵存储的无向图的深度优先遍历 template <typename Type> void Graph<Type>::DFS() { stack<int> iStack; showVertex(0); vertexList[0]->wasVisted = true; iStack.push

HDU 3085 Nightmare Ⅱ【BFS +曼哈顿距离+综合性较强】

Nightmare Ⅱ Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 16   Accepted Submission(s) : 6 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Last night, little erriyue

DFS BFS代码

#define maxnum 30 #include<bits_stdc++.h> int visited[maxnum]={0}; using namespace std; typedef struct bian//边 { int mark;//标记是否搜索 int ivex,jvex;//两顶点位置 bian *ilink,*jlink;//指向两顶点的其他边 int info;//信息 } bian,*pbian; typedef struct dian//点 { char name;

第七十四课 图的遍历(BFS)

广度优先相当于对顶点进行分层,层次遍历. 在Graph.h中添加BFS函数: 1 #ifndef GRAPH_H 2 #define GRAPH_H 3 4 #include "Object.h" 5 #include "SharedPointer.h" 6 #include "Array.h" 7 #include "DynamicArray.h" 8 #include "LinkQueue.h" 9 1