Codeforces Gym - 101755H Save Path (BFS)

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

·>原题

先理解一下大意:给出一个 n*m 大小的地图。你将由 S(start) 处出发,到达 F(finish) 处为结束。而现在地图上有一些 M(Monster) ,它们的攻击移动步数为 d ;你和怪物们都只能四向移动(上下左右,不能斜向),每次单格移动算一步,一旦踏入怪物的攻击范围你就会当场去世。你需要活着从起点移动到终点,请判断是否存在这样的安全路径,若存在,输出最短路径的步数;若不存在,输出 -1
注意,即使是起点和终点,只要在怪物的攻击范围内,你也会被怪物杀死。(没有游戏体验的垃圾游戏)

分析

这道题是之前打过觉得比较有代表性的题目。题目乍看似乎贴几个bfs模板改改就能过了,但是对我这种菜鸡来说坑点太多,错得停不下来;

首先,题目只给出了 n*m <=200000 这个地图限制条件,也就是说你需要开设动态二维数组。 200000×200000 的数组因为限制根本开不出来,就算开的出来也肯定会内存爆炸;这里我用了 vector 类型,通过 resize() 来进行动态内存分配;

数组开好并输入后,用 bfs 把每个怪物的可到达距离都换成墙壁,记得不要忘记怪物本身也是墙壁。(可以在一开始动态地开设一个新的二维整形数组,把怪物所在的点标记成 d+1 ,然后每行进一步就减 1 ,一直行进到 1 为止,也可以直接在输入的时候把必要数据都保存在这个二维数组里面,节省一些内存空间)

然后直接从起点开始 bfs ,需要特别判断起点在不在怪物的攻击范围内,到达终点(同样需要判断在不在攻击范围内,不过采取先判断是否为墙壁再进行移动的方案可以忽略这个问题)或者无路可走后查看终点记录的步数情况。

跑一下样例,过了。提交,答案错误。

原因找了很久,最后发现是一个对于本人这种对 bfs 不熟悉的新手会犯的错误。我把怪物的攻击范围通过 bfs 换成墙壁这个操作分步进行,也就是每进行一只怪物的攻击范围判断便进行一次 bfs ,而这和判断条件有所冲突;

如图,此时 d 为 3 ;

先进行单个 M 的判断会使得下面 bfs 的路径被切断,从而本该在下面怪物攻击范围的终点没有被相应的攻击范围所覆盖。

也就是说所有怪物的 bfs 需要同时进行

修正后:


灰色部分表示二者的重合部分,此时的每个 M 的 bfs 受到其他部分的影响(可能有点不准确),此时我们可以注意到终点已被覆盖。

如此修改之后便过了,但是代码仍有很多多余的部分。

下面是很难看的AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define INF 10000000
const int MAXN=200005;
vector<char> inp[MAXN];
vector<int> wall[MAXN],step[MAXN];	
char cut[MAXN];
int sx,sy;
int gx,gy;
int mx[4]={1,0,-1,0},my[4]={0,1,0,-1};
int n,m,d;
typedef pair<int,int> P;
queue<P> p;
void wallbuildbfs(){
    while(!p.empty()){
        P f=p.front();p.pop();
        for(int i=0;i<4;i++){
            int nx=f.first+mx[i],ny=f.second+my[i];
            if(wall[f.first][f.second]>1&&0<=nx&&nx<n&&0<=ny&&ny<m&&wall[nx][ny]==0){
                p.push(P(nx,ny));
                wall[nx][ny]=wall[f.first][f.second]-1;
            }
        }
    }
    return ;
}

int bfs(){
    queue<P> que;
    while(!que.empty()) que.pop();
    if(wall[sx][sy]!=0) return -1;
    que.push(P(sx,sy));
    step[sx][sy]=0;
    while(!que.empty()){
        P f=que.front();que.pop();
        if(f.first==gx&&f.second==gy) return step[gx][gy];
        for(int i=0;i<4;i++){
            int nx=f.first+mx[i],ny=f.second+my[i];
            if(0<=nx&&nx<n&&0<=ny&&ny<m&&step[nx][ny]==INF&&wall[nx][ny]==0){
                que.push(P(nx,ny));
                step[nx][ny]=step[f.first][f.second]+1;
            }
        }
    }
    return -1;
}

int main(){
    int ans;
    scanf("%d%d%d",&n,&m,&d);
     for(int i=0;i<n;i++){			//动态二维数组
            wall[i].resize(m+1);
            inp[i].resize(m+1);
            step[i].resize(m+1);
     }
     for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            wall[i][j]=0;
            step[i][j]=INF;
        }
    for(int i=0;i<n;i++){
            scanf("%s",cut);
            inp[i].push_back(0);	//处理数组尾
            for(int j=0;j<m;j++){
                inp[i][j]=cut[j];
            }
    }
    while(!p.empty()) p.pop();
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(inp[i][j]=='S'){
                sx=i;sy=j;
            }
            if(inp[i][j]=='F'){
                gx=i;gy=j;
            }
            if(inp[i][j]=='M'){
                p.push(P(i,j));
                wall[i][j]=d+1;
            }
        }
    }
    wallbuildbfs();
    ans=bfs();
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43515064/article/details/86684485