Gym - 101755H Safe Path

本文介绍了一个游戏地图中从起点到终点的安全路径规划算法。通过DFS预处理怪物的危险区域,并利用BFS寻找最短安全路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 nm 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
Copy
5 7 1
S.M...M
.......
.......
M...M..
......F
output
Copy
12
input
Copy
7 6 2
S.....
...M..
......
.....M
......
M.....
.....F
output
Copy
11
input
Copy
7 6 2
S.....
...M..
......
......
.....M
M.....
.....F
output
Copy
-1
input
Copy
4 4 2
M...
.S..
....
...F
output
Copy
-1
Note

Please note that monsters can run and kill you on start cell and on finish cell as well.


这道题其实数据还是有点水,用vector居然也能过,下次一定转换为一维做。

题意就是n*m的方格里面能不能到达终点,有意思的是怪兽有攻击方位,先DFS标记处怪兽的攻击范围,然后BFS最短路就解决。

关键点是DFS注意优化不然20组45组数据要超时。


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<string.h>
#include <vector>
#include<queue>
const int maxn=200005;
using namespace std;
vector<char>mat[maxn];
int v[maxn];
int sx,sy,ex,ey;
int m,n,t;
int dirx[]= {-1,1,0,0};
int diry[]= {0,0,1,-1};
struct node
{
    int x,y;
} no,ne;
int flag=0;
bool check(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
        return true;
    return false;
}
void DFS(int x,int y,int d)
{
    if(d==0||flag==1)
        return ;
    for(int i=0; i<4; i++)
    {
        int fx=dirx[i]+x;
        int fy=diry[i]+y;
        if((fx==ex&&fy==ey)||(fx==sx&&fy==sy))
        {
            flag=1;
            return ;//如果起点或者终点在攻击范围直接结束程序
        }
        if(check(fx,fy))
        {
            if(mat[fx][fy]=='M')
            {
                continue;
            }

            if( v[fx*m+fy]+1<v[x*m+y])//如果即将搜索的点的扩散范围大于这次搜索过来的范围就不管,减少时间复杂度。
            {
                v[fx*m+fy]=d-1;//如果这个点的当前扩散范围小于即将更新过来的范围就更新。注意每个点不只是搜索一次
                DFS(fx,fy,d-1);
            }


        }
    }
}
int BFS()
{
    queue<node>q;
    no.x=sx,no.y=sy;
    q.push(no);
    v[sx*m+sy]=0;
    while(!q.empty())
    {
        no=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            ne.x=dirx[i]+no.x;
            ne.y=diry[i]+no.y;
            if(check(ne.x,ne.y))
            {
                if(v[ne.x*m+ne.y]==-1)
                {
                    v[ne.x*m+ne.y]=v[no.x*m+no.y]+1;
                    q.push(ne);
                }
            }
        }
    }
    return v[ex*m+ey];
}
int main()
{
    int d;
    char s;
    scanf("%d %d %d",&n,&m,&d);
    t=d;
    for(int i=0; i<n; i++)
    {
        getchar();
        for(int j=0; j<m; j++)
        {
            scanf("%c",&s);
            if(s=='S')
            {
                sx=i,sy=j;
            }
            if(s=='F')
                ex=i,ey=j;
            mat[i].push_back(s);
            v[i*m+j]=-1;
        }
    }


    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(mat[i][j]=='M')
            {
                if(v[i*m+j]==d)
                    continue;
                v[i*m+j]=d;
                DFS(i,j,d);
            }
        }
        if(flag==1)
            break;
    }

    if(flag==1)
        cout<<-1<<endl;
    else
    {
        int ans = BFS();
        cout<<ans<<endl;
    }

    return 0;
}

zhu@zhu:~/桌面/zhu/gym-duckietown$ python3 manual_control.py --env-name Duckietown Traceback (most recent call last): File "manual_control.py", line 16, in <module> from gym_duckietown.envs import DuckietownEnv File "/home/zhu/桌面/zhu/gym-duckietown/gym_duckietown/envs/__init__.py", line 4, in <module> from gym_duckietown.envs.duckietown_env import DuckietownEnv File "/home/zhu/桌面/zhu/gym-duckietown/gym_duckietown/envs/duckietown_env.py", line 5, in <module> from ..simulator import Simulator File "/home/zhu/桌面/zhu/gym-duckietown/gym_duckietown/simulator.py", line 8, in <module> import geometry File "/home/zhu/.local/lib/python3.8/site-packages/geometry/__init__.py", line 48, in <module> from .basic_utils import * File "/home/zhu/.local/lib/python3.8/site-packages/geometry/basic_utils.py", line 4, in <module> from contracts import contract, new_contract File "/home/zhu/.local/lib/python3.8/site-packages/contracts/__init__.py", line 44, in <module> from .useful_contracts import * File "/home/zhu/.local/lib/python3.8/site-packages/contracts/useful_contracts/__init__.py", line 8, in <module> from .numpy_specific import * File "/home/zhu/.local/lib/python3.8/site-packages/contracts/useful_contracts/numpy_specific.py", line 8, in <module> def finite(x): File "/home/zhu/.local/lib/python3.8/site-packages/contracts/__init__.py", line 28, in new_contract return new_contract_main(*args) File "/home/zhu/.local/lib/python3.8/site-packages/contracts/main.py", line 553, in new_contract new_contract_impl(identifier, function) File "/home/zhu/.local/lib/python3.8/site-packages/contracts/main.py", line 563, in new_contract_impl from .syntax import ParseException File "/home/zhu/.local/lib/python3.8/site-packages/contracts/syntax.py", line 91, in <module> from .library import (EqualTo, Unary, Binary, composite_contract, File "/home/zhu/.local/lib/python3.8/site-packages/contracts/library/__init__.py", line 29, in <module> from .array import (ShapeContract, Shape, Array, ArrayConstraint, DType, File "/home/zhu/.local/lib/python3.8/site-packages/contracts/library/array.py", line 6, in <module> from .array_ops import (ArrayOR, ArrayAnd, DType, ArrayConstraint, File "/home/zhu/.local/lib/python3.8/site-packages/contracts/library/array_ops.py", line 244, in <module> 'np_complex': np.complex, # Shorthand for complex128. File "/home/zhu/.local/lib/python3.8/site-packages/numpy/__init__.py", line 305, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'complex'. `np.complex` was a deprecated alias for the builtin `complex`. To avoid this error in existing code, use `complex` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.complex128` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值