• NC15665 maze


    题目链接

    题目

    题目描述

    小明来到一个由n x m个格子组成的迷宫,有些格子是陷阱,用'#'表示,小明进入陷阱就会死亡,'.'表示没有陷阱。小明所在的位置用'S'表示,目的地用'T'表示。

    小明只能向上下左右相邻的格子移动,每移动一次花费1秒。

    有q个单向传送阵,每个传送阵各有一个入口和一个出口,入口和出口都在迷宫的格子里,当走到或被传送到一个有传送阵入口的格子时,小明可以选择是否开启传送阵。如果开启传送阵,小明就会被传送到出口对应的格子里,这个过程会花费3秒;如果不开启传送阵,将不会发生任何事情,小明可以继续向上下左右四个方向移动。

    一个格子可能既有多个入口,又有多个出口,小明可以选择任意一个入口开启传送阵。使用传送阵是非常危险的,因为有的传送阵的出口在陷阱里,如果小明使用这样的传送阵,那他就会死亡。也有一些传送阵的入口在陷阱里,这样的传送阵是没有用的,因为小明不能活着进入。请告诉小明活着到达目的地的最短时间。

    输入描述

    有多组数据。对于每组数据:
    第一行有三个整数n,m,q(2≤ n,m≤300,0≤ q ≤ 1000)。
    接下来是一个n行m列的矩阵,表示迷宫。
    最后q行,每行四个整数x1,y1,x2,y2(0≤ x1,x2< n,0≤ y1,y2< m),表示一个传送阵的入口在x1行y1列,出口在x2行y2列。

    输出描述

    如果小明能够活着到达目的地,则输出最短时间,否则输出-1。

    示例1

    输入

    5 5 1
    ..S..
    .....
    .###.
    .....
    ..T..
    1 2 3 3
    5 5 1
    ..S..
    .....
    .###.
    .....
    ..T..
    3 3 1 2
    5 5 1
    S.#..
    ..#..
    ###..
    .....
    ....T
    0 1 0 2
    4 4 2
    S#.T
    .#.#
    .#.#
    .#.#
    0 0 0 3
    2 0 2 2
    折叠

    输出

    6
    8
    -1
    3

    备注

    坐标从0开始

    题解

    方法一

    知识点:BFS,优先队列。

    显然用bfs,但要做修正的是把队列更换为优先队列,因为传送门的存在使得步数时间线被破坏,先到的点不一定步数比后到的点少,因此优先队列维护步数从小到大扩展。但就不能每次扩展直接锁点了,要在每个点真正经过的时候才考虑是否锁点,如果被之前的经过了,则跳过。

    细节上注意传送门的存取,用入口作为下标,多个出口用 vector 存储,形成一个 vector 的二维数组,可以方便扩展。

    遇到出口直接跳出即可,因为优先队列维护了时间。

    时间复杂度 O(?)

    空间复杂度 O(mn)

    方法二

    知识点:BFS。

    如果不用优先队列也可以做,每次扩展如果扩展的点时间更小不扩展,否则覆盖时间。与优先队列的区别在于,这种做法要遍历地图才可以得到结果。

    时间复杂度 O(?)

    空间复杂度 O(mn)

    代码

    方法一

    #include
    using namespace std;
    int n, m, q;
    char dt[307][307];
    bool vis[307][307];
    const int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
    vector<pair<int, int>> tsm[307][307];
    struct node {
    int x, y;
    int step;
    friend bool operator<(const node &a, const node &b) {
    return a.step > b.step;
    }
    };
    int bfs(node init) {
    priority_queue pq;
    pq.push(init);
    while (!pq.empty()) {
    node cur = pq.top();
    pq.pop();
    if (vis[cur.x][cur.y]) continue;///但一定是最短的时候经过
    vis[cur.x][cur.y] = 1;
    if (dt[cur.x][cur.y] == 'T') return cur.step;
    for (int i = 0;i < 4;i++) {
    int xx = cur.x + dir[i][0];
    int yy = cur.y + dir[i][1];
    if (xx < 0 || xx >= n || yy < 0 || yy >= m || vis[xx][yy] || dt[xx][yy] == '#') continue;
    pq.push({ xx,yy,cur.step + 1 });///扩展出来的点不一定是最短的,不能在这里vis
    }
    for (auto tr : tsm[cur.x][cur.y]) {
    if (vis[tr.first][tr.second] || dt[tr.first][tr.second] == '#') continue;
    pq.push({ tr.first,tr.second,cur.step + 3 });
    }
    }
    return -1;
    }
    int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    while (cin >> n >> m >> q) {
    memset(vis, 0, sizeof(vis));
    node init;
    for (int i = 0;i < n;i++) {
    for (int j = 0;j < m;j++) {
    cin >> dt[i][j];
    if (dt[i][j] == 'S') init.x = i, init.y = j, init.step = 0;
    }
    }
    for (int i = 0;i < q;i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    tsm[x1][y1].push_back({ x2,y2 });
    }
    cout << bfs(init) << '\n';
    for (int i = 0;i < n;i++)
    for (int j = 0;j < m;j++)
    tsm[i][j].clear();
    }
    return 0;
    }
    折叠

    方法二

    #include
    using namespace std;
    int n, m, q;
    char dt[307][307];
    int vis[307][307];///记录步数,用小的替换大的
    const int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
    vector<pair<int, int>> tsm[307][307];
    struct node {
    int x, y;
    };
    void bfs(node init) {
    queue q;
    q.push(init);
    vis[init.x][init.y] = 0;
    int ans = -1;
    while (!q.empty()) {
    node cur = q.front();
    q.pop();
    for (int i = 0;i < 4;i++) {
    int xx = cur.x + dir[i][0];
    int yy = cur.y + dir[i][1];
    if (xx < 0 || xx >= n || yy < 0 || yy >= m || vis[xx][yy] <= vis[cur.x][cur.y] + 1 || dt[xx][yy] == '#') continue;
    q.push({ xx,yy });
    vis[xx][yy] = vis[cur.x][cur.y] + 1;
    }
    for (auto tr : tsm[cur.x][cur.y]) {
    if (vis[tr.first][tr.second] <= vis[cur.x][cur.y] + 3 || dt[tr.first][tr.second] == '#') continue;
    q.push({ tr.first,tr.second });
    vis[tr.first][tr.second] = vis[cur.x][cur.y] + 3;
    }
    }
    }
    int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    while (cin >> n >> m >> q) {
    memset(vis, 0x3f, sizeof(vis));
    node init, ans;
    for (int i = 0;i < n;i++) {
    for (int j = 0;j < m;j++) {
    cin >> dt[i][j];
    if (dt[i][j] == 'S') init.x = i, init.y = j;
    if (dt[i][j] == 'T') ans.x = i, ans.y = j;
    }
    }
    for (int i = 0;i < q;i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    tsm[x1][y1].push_back({ x2,y2 });
    }
    bfs(init);
    cout << (vis[ans.x][ans.y] > 3e5 ? -1 : vis[ans.x][ans.y]) << '\n';
    for (int i = 0;i < n;i++)
    for (int j = 0;j < m;j++)
    tsm[i][j].clear();
    }
    return 0;
    }
    折叠
  • 相关阅读:
    LeetCode --- 2068. Check Whether Two Strings are Almost Equivalent 解题报告
    【React学习】React高级特性
    一款低 EMI,无需滤波器, AB/D 类可选式音频功率放大器 SL2018
    编程猫创作工具:新版Kitten新体验
    Java基础|Java并发相关api
    制作一个简单HTML个人网页网页(HTML+CSS)大话西游之大圣娶亲电影网页设计
    AI系统源码ChatGPT网站源码+ai绘画系统/支持GPT4.0/支持Midjourney局部编辑重绘
    OpenStack 创建虚拟机错误: Host ‘compute1‘ is not mapped to any cell
    计算机毕设 LSTM的预测算法 - 股票预测 天气预测 房价预测
    【附源码】计算机毕业设计SSM商品推荐系统
  • 原文地址:https://www.cnblogs.com/BlankYang/p/16484830.html