跳到正文

题解

云梦谣题解

Bronya16A约 8 分钟
原文
文章信息与版本

文章信息

发布
2023年2月15日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 8 分钟
题目
P9065

版本

  1. v1当前

题目传送门

题目分析

这题是一个 bfs 走迷宫问题。

思路是先判断如果 k≤1k\le1 那么就直接 bfs 走迷宫就可以了。

如果k>1的情况下就要先找离起点最近的传送点们和起点到它们的距离,把它们的高度记录到一个数组里,把起点到它们的距离记在一个变量里。

再找离终点最近的传送点们,把它们的高度记录到一个数组里,把终点到它们的距离记在一个变量里。

然后再从起点走到终点(不传送的那种),把距离也记下来。

然后如果起点到终点里不可以传送的话,输出从起点走到终点(不传送的那种)的距离.

否则到两个高度数组里找有没有高度相同的,如果有,就把从起点走到终点(不传送的那种)的距离和从起点到离起点最近的传送点的距离加上从终点到离终点最近的传送点的距离加一取最小值后输出。

如果没有,就把从起点走到终点(不传送的那种)的距离和从起点到离起点最近的传送点的距离加上从终点到离终点最近的传送点的距离加二取最小值后输出。

下面是完整代码:

C++
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll h[4000][4000];
ll n, m, k, minn = 1e10;
bool vis[4000][4000];
ll dx[20] = {1, -1, 0, 0};
ll dy[20] = {0, 0, 1, -1};
struct node {
    ll x, y, f, z;
};
ll cx[9000003], cy[9000003];
ll a[4000][4000];
vector<ll> h2,l1h,l2h;
void bfs(ll x, ll y, ll f) {
    memset(vis,0,sizeof(vis));
    h2.clear();
    minn=1e10;
    queue<node> q;
    node tmp;
    tmp.x = x;
    tmp.y = y;
    tmp.f = f;
    q.push(tmp);
    while (!q.empty()) {
        node u = q.front();
        q.pop();
        if (h[u.x][u.y] == 0) {
            continue;
        }
        if (a[u.x][u.y] == 1) {
            if (u.f == minn) {
                h2.push_back(h[u.x][u.y]);
            } else if (u.f < minn) {
                minn = u.f;
                h2.clear();
                h2.push_back(h[u.x][u.y]);
            }
        } else {
            if(u.f>minn){
                continue;
            }
            vis[u.x][u.y]=1;
            for (int i = 0; i < 4; i++) {
                ll nx = u.x + dx[i], ny = u.y + dy[i];
                if (h[nx][ny] == 0 || nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny]) {
                    continue;
                }
                vis[nx][ny] = 1;
                node tmp;
                tmp.x = nx;
                tmp.y = ny;
                tmp.f = u.f + 1;
                q.push(tmp);
            }
        }
    }
}
void bfs2(ll x, ll y, ll f) {
    minn=1e10;
    queue<node> q;
    node tmp;
    tmp.x = x;
    tmp.y = y;
    tmp.f = f;
    vis[x][y]=1;
    q.push(tmp);
    while (!q.empty()) {
        node u = q.front();
        q.pop();
        if (u.f > minn) {
            continue;
        }
        if (u.x == n && u.y == m) {
            minn = min(minn, u.f);
            continue;
        }
        for (int i = 0; i < 4; i++) {
            ll nx = u.x + dx[i], ny = u.y + dy[i];
            if (h[nx][ny] == 0 || nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny]) {
                continue;
            }
            vis[nx][ny] = 1;
            tmp.x = nx;
            tmp.y = ny;
            tmp.f = u.f + 1;
            q.push(tmp);
        }
    }
}
int main() {
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%lld",&h[i][j]);
        }
    }
    for (int i = 1; i <= k; i++) {
        scanf("%lld%lld",&cx[i],&cy[i]);
        a[cx[i]][cy[i]] = 1;
    }
    if(k<=1){
        bfs2(1,1,0);
        if(minn==1e10){
            cout<<"-1";
        }else{
            cout<<minn;
        }
    }else{
        bfs(1,1,0);
        ll l1,l2;
        l1=minn;
        for (int i=0;i<h2.size();i++){
            l1h.push_back(h2[i]);
        }
        bfs(n,m,0);
        l2=minn;
        for (int i=0;i<h2.size();i++){
            l2h.push_back(h2[i]);
        }
        bfs2(1,1,0);
        if(l1==l2&&l1==1e10){
            cout<<"-1";
        }else{
            for (int i=0;i<l1h.size();i++){
                for (int j=0;j<l2h.size();j++){
                    if(l1h[i]==l2h[j]){
                        minn = min(minn, l1 + l2 + 1);
                        cout<<minn;
                        return 0;
                    }
                }
            }
            minn=min(minn,l1+l2+2);
            cout<<minn;
            return 0;
        }
    }
    return 0;
}

求管理员大大通过

原文