题解:洛谷 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;
}
相关推荐
dying_man43 分钟前
LeetCode--42.接雨水
算法·leetcode
vortex51 小时前
算法设计与分析 知识总结
算法
艾莉丝努力练剑2 小时前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(三)
c语言·开发语言·数据结构·学习·算法
ZZZS05162 小时前
stack栈练习
c++·笔记·学习·算法·动态规划
hans汉斯2 小时前
【人工智能与机器人研究】基于力传感器坐标系预标定的重力补偿算法
人工智能·算法·机器人·信号处理·深度神经网络
vortex53 小时前
算法设计与分析:分治、动态规划与贪心算法的异同与选择
算法·贪心算法·动态规划
前端拿破轮4 小时前
🤡🤡🤡面试官:就你这还每天刷leetcode?连四数相加和四数之和都分不清!
算法·leetcode·面试
地平线开发者4 小时前
征程 6|工具链量化简介与代码实操
算法·自动驾驶
DoraBigHead5 小时前
🧠 小哆啦解题记——谁偷改了狗狗的台词?
算法
Kaltistss5 小时前
240.搜索二维矩阵Ⅱ
线性代数·算法·矩阵