题解:洛谷 P2199 最后的迷宫

题目https://www.luogu.com.cn/problem/P2199

显然,数据最大 ,数组我们开不下,动态开数组。

对于每一个查询,从起点开始,走一步判断是否能看到火焰杯。

如果已经没法走了,直接拆墙,输出 Poor Harry。

实现

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int n,m,Bx,By,Ex,Ey,xux[8][3]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}},xyx[5][3]={{-1,0},{1,0},{0,1},{0,-1}};
vector<char>a[16385];
bool check(int dx,int dy){
    for(int i=0;i<8;i++){
        int af=dx,bt=dy;
        while(true){
            if(af<1||bt<1||af>n||bt>m||a[af][bt]=='X'){
                break;
            }
            if(af==Ex&&bt==Ey){
                return true;
            }
            af+=xux[i][0],bt+=xux[i][1];
        }
    }
    return false;
}
int bfs(int Bx,int By){
    vector<int>dis[n+5];
    vector<bool>vis[n+5];
    for(int i=1;i<=n+3;i++){
        for(int j=0;j<=m+1;j++){
            dis[i].push_back(0);
            vis[i].push_back(false);
        }
    }
    queue<pair<int,int>>q;
    q.push({Bx,By});
    vis[Bx][By]=true;
    while(q.size()){
        auto p=q.front();
        q.pop();
        if(check(p.first,p.second)){
            return dis[p.first][p.second];
        }
        for(int i=0;i<4;i++){
            int dx=p.first+xyx[i][0],dy=p.second+xyx[i][1];
            if(dx&&dy&&dx<=n&&dy<=m&&!vis[dx][dy]&&a[dx][dy]!='X'){
                dis[dx][dy]=dis[p.first][p.second]+1,vis[dx][dy]=true;
                q.push({dx,dy});
            }
        }
    }
    return -1;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        a[i].push_back(0);
        for(int j=1;j<=m;j++){
            char x;
            cin>>x;
            a[i].push_back(x);
        }
    }
    while(true){
        cin>>Ex>>Ey>>Bx>>By;
        if(!Bx||!By||!Ex||!Ey){
            return 0;
        }
        int tmp=bfs(Bx,By);
        cout<<(tmp==-1?"Poor Harry":to_string(tmp))<<'\n';
    }
    return 0;
}

P.S.:呃......我写的时候把方向写错了。

解法2

,可以考虑用一维数组进行存储。

我们进行"降维打击",从二维坐标转化为一维坐标,比如说有一个网格:

|-------------------------------------|-------------------------------------|-------------------------------------|-------------------------------------|
| | | | |
| | | | |
| | | | |
| | | | |

将其转化为一维坐标:

|----------------------------------|----------------------------------|----------------------------------|----------------------------------|
| | | | |
| | | | |
| | | | |
| | | | |

具体来说,对于一个二维坐标 ,其一维坐标为

其他的地方就跟二维的写法一样了。

实现

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int n,m,Bx,By,Ex,Ey,dis[17005],xux[8][3]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}},xyx[5][3]={{-1,0},{1,0},{0,1},{0,-1}};
char a[17005];
bool vis[17005];
int XY(int dx,int dy){
    return (dx-1)*m+dy;
}
bool check(int dx,int dy){
    for(int i=0;i<8;i++){
        int af=dx,bt=dy;
        while(true){
            if(af<1||bt<1||af>n||bt>m||a[XY(af,bt)]=='X'){
                break;
            }
            if(af==Ex&&bt==Ey){
                return true;
            }
            af+=xux[i][0],bt+=xux[i][1];
        }
    }
    return false;
}
int bfs(int Bx,int By){
    fill(vis,vis+n*m+5,false);
    queue<pair<int,int> >q;
    q.push({Bx,By});
    vis[XY(Bx,By)]=true,dis[XY(Bx,By)]=0;
    while(q.size()){
        auto p=q.front();
        q.pop();
        if(check(p.first,p.second)){
            return dis[XY(p.first,p.second)];
        }
        for(int i=0;i<4;i++){
            int dx=p.first+xyx[i][0],dy=p.second+xyx[i][1];
            if(dx&&dy&&dx<=n&&dy<=m&&!vis[XY(dx,dy)]&&a[XY(dx,dy)]!='X'){
                dis[XY(dx,dy)]=dis[XY(p.first,p.second)]+1,vis[XY(dx,dy)]=true;
                q.push({dx,dy});
            }
        }
    }
    return -1;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[XY(i,j)];
        }
    }
    while(true){
        cin>>Ex>>Ey>>Bx>>By;
        if(!Bx||!By||!Ex||!Ey){
            return 0;
        }
        int tmp=bfs(Bx,By);
        cout<<(tmp==-1?"Poor Harry":to_string(tmp))<<'\n';
    }
    return 0;
}
相关推荐
To_OC11 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
delishcomcn12 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹12 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
雪碧聊技术12 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
CV-Climber15 小时前
检索技术的实际应用
人工智能·算法
hhzz16 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_16 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
TsingtaoAI17 小时前
3D高斯泼溅技术发展及其在具身智能领域的应用综述
人工智能·算法·ai·具身智能·高斯泼溅
atunet17 小时前
关于图算法中的连通分量检测与最小割问题7
算法