RPG打怪//Gym - 101755H//bfs

RPG打怪//Gym - 101755H//bfs


题目

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
Note
Please note that monsters can run and kill you on start cell and on finish cell as well.
题意
怪有攻击范围,从起点到终点最短距离。
链接:http://codeforces.com/gym/101755/problem/H

思路

先用宽搜把怪能到的地方刷成怪,然后再进行宽搜走迷宫。

代码

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef pair<int ,int >P;

const int N = 200020;
char mp[N];
int dis[N];
int n,m,a,sx,sy,fx,fy;
int nx[4]={-1,0,1,0};
int ny[4]={0,-1,0,1};
int check(int x,int y){
    if(x>=0&&x<n&&y>=0&&y<m) return 1;
    return 0;
}
queue<P>quee;
void bfs(){                                                   //对怪物宽搜把可到达的地方-1
    while(quee.size()){
        P d = quee.front();quee.pop();
        if(dis[d.first*m+d.second]==0)continue;
        for(int i=0;i<4;i++){
            int dx=d.first+nx[i],dy=d.second+ny[i];
            if(check(dx,dy)){
                if(dis[dx*m+dy]<dis[d.first*m+d.second]-1){
                    dis[dx*m+dy]=dis[d.first*m+d.second]-1;
                    quee.push(P(dx,dy));
                }
            }
        }
    }
}
void bfs2(int x,int y){
    queue<P>que;
    que.push(P(x,y));
    dis[x*m+y]=0;
    while(que.size()){
        P d=que.front();que.pop();
        for(int i=0;i<4;i++){
            int dx=d.first+nx[i],dy=d.second+ny[i];
            if(check(dx,dy)){
                if(dis[dx*m+dy]==-1){                              //怪物到不了的地方都是-1
                    dis[dx*m+dy]=dis[d.first*m+d.second]+1;
                    que.push(P(dx,dy));
                }
            }
        }
    }
}
int main(){
    cin>>n>>m>>a;
    getchar();
    memset(dis,-1,sizeof(dis));
    for(int i=0;i<n;i++){
        scanf("%s",&mp[i*m]);
    }
    sx = -1,sy = -1,fx=-1,fy = -1;
    while(quee.size())quee.pop();
    for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        if(mp[i*m+j]=='.') continue;
        else if(mp[i*m+j]=='M'){              //把怪的位置用队列记录
            quee.push(P(i,j));
            dis[i*m+j] = a;
        }
        else if(mp[i*m+j]=='S'){
            sx=i,sy=j;
        }
        else if(mp[i*m+j]=='F'){
            fx=i,fy=j;
        }
    }
}
    bfs();
    if(sx==-1||sy==-1||fx==-1||fy==-1||dis[sx*m+sy]!=-1||dis[fx*m+fy]!=-1){  //起点终点有怪物就输出-1
        cout<<"-1"<<endl;
        return 0;
    }
    bfs2(sx,sy);
    if(dis[fx*m+fy]==-1)
        cout<<"-1"<<endl;
    else
        cout<<dis[fx*m+fy]<<endl;
    return 0;
}


注意

用队列存入怪的坐标,减少循环次数,防止tle。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值