Codeforces Gym - 101755H Save Path (BFS)

H. Safe Path

time limit per test:2.0 smemory limit per test:256 MB
input:standard inputoutput: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值