走迷宫问题——边的权重不一定相同

shwei edited 4 年,3 月前

1.边的权值相同

给定一个$n\times m$的二维整数数组,用来表示一个迷宫,数组中只包含0或1,其中0表示可以走的路,1表示不可通过的墙壁。

最初,有一个人位于左上角(1, 1)处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。

请问,该人从左上角移动至右下角(n, m)处,至少需要移动多少次。

数据保证(1, 1)处和(n, m)处的数字为0,且一定至少存在一条通路。

输入格式

第一行包含两个整数n和m。

接下来n行,每行包含m个整数(0或1),表示完整的二维数组迷宫。

输出格式

输出一个整数,表示从左上角移动至右下角的最少移动次数。

数据范围

$1≤n,m≤100$

输入样例:

5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

输出样例:

8

算法 (BFS)

C++ 代码

// 迷宫路径 BFS + 输出最短路径
// g表示图 d表示距离
// d[0][0] = 0 表示取过这个位置了
// 用一个pr数组存前一个就行了, 路径
// BFS第一次搜到的才是最短距离, 不然第二次搜到就不是最短距离 就没必要去处理他了

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 110;

int n, m;
int g[N][N], d[N][N];                       // 一般都设为全局变量, pre[N][N]用来存路径 因为每个点只来源于一个点
PII pre[N][N];                              // DFS不用这样存,每一条都直接输出,所以空间是0(n)


int bfs()
{
    queue<PII> q;
    memset(d, -1, sizeof d);
    d[0][0] = 0;                            // d[x][y] != -1 表示该点被搜索过了
    q.push({0, 0});
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

    while (q.size())
    {
        auto t = q.front();
        q.pop();

        for (int i = 0; i < 4; i ++ )
        {
            int x = t.first + dx[i], y = t.second + dy[i];

            if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
            {
                pre[x][y] = {t.first, t.second};
                d[x][y] = d[t.first][t.second] + 1;
                q.push({x, y});
            }
        }
    }

    // 逆序输出路径
    int x = n - 1, y = m - 1;
    while (x || y) {
        auto t = pre[x][y];   
        cout << x << " " << y << endl; 
        x = t.first, y = t.second;
    }
    cout << 0 << " " << 0 << endl;


    return d[n - 1][m - 1];
}


int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < m; j ++ )
            cin >> g[i][j];

    cout << bfs() << endl;

    return 0;
}

2.边的权值不同

题目描述:
单点时限: 2.0 sec
内存限制: 256 MB
一天,sunny 不小心进入了一个迷宫,不仅很难寻找出路,而且有的地方还有怪物,但是 sunny 有足够的能力杀死怪物,但是需要一定的时间,但是 sunny 想早一点走出迷宫,所以请你帮助他计算出最少的时间走出迷宫,输出这个最少时间。

我们规定每走一格需要时间单位 1, 杀死怪物也需要时间 1, 如果不能走到出口,则输出 impossible. 每次走只能是上下左右 4 个方向。

输入格式
每次首先 2 个数 n,m (0<n,m≤200),代表迷宫的高和宽,然后 n 行,每行 m 个字符。

S 代码你现在所在的位置。
T 代表迷宫的出口。
#代表墙,你是不能走的。
X 代表怪物。
. 代表路,可以走。
处理到文件结束。

输出格式
输出最少的时间走出迷宫。不能走出输出 impossible。

输入样例:

4 4
S.X.
#..#
..#.
X..T
4 4
S.X.
#..#
..#.
X.#T

输出样例:

6
impossible

算法
(BFS + 优先队列)

题意:走迷宫,求最短路径,上下左右走一格花费1,走到有怪的格子花费2.

思路:
将每一点的坐标和由起点到该点的距离存入结构体.
由起点开始,将该点存入优先队列,以到起点的距离dis为优先级,每次取出dis最小的,向外扩散。
相当于第一轮得出所有到起点距离为1的点,第二轮得出所有到起点距离为2的点。

注意:对普通的最短路问题,由于每个各自的花费相同,因此每次存入的点优先级都相同.
故不需要使用优先队列,但本题存在有无怪物的区别,每次存入的格子的优先级可能不同,故使用优先队列。

C++ 代码

#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
char maze[201][201];
int sx, sy, tx, ty;
//左右上下4个方向
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int m, n;

struct node {
    int x,  y, dis;
};

bool operator < (const node & a, const node & b) {
    return a.dis > b.dis;
}

void bfs() {
    priority_queue<node> que;
    node st { sx,sy,0 };
    maze[sx][sy] = '#';
    que.push(st);

    while (!que.empty()) {
        node p = que.top();
        que.pop();
        //若已找到,则退出
        if (p.x == tx && p.y == ty) {
            cout << p.dis << endl;
            return;
        }
        for (int i = 0; i < 4; ++i) {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];
            node np{ nx,ny, 0};

            if (nx >= 0 && nx < n&&ny >= 0 && ny < m&&maze[nx][ny] != '#') {
                if (maze[nx][ny] == 'X')
                    np.dis = p.dis + 2;
                else
                    np.dis = p.dis + 1;
                maze[np.x][np.y] = '#';
                que.push(np);

            }
        }
    }
    printf("impossible\n");
}
int main() {
    while (cin>>n>>m) {
        for (int i = 0; i < n; i++)
            scanf("%s", maze[i]);
        for(int i=0; i<n; i++)
            for (int j = 0; j < m; j++) {
                if (maze[i][j] == 'S')
                    sx = i, sy = j;
                else if (maze[i][j] == 'T')
                    tx = i, ty = j;
            }
        bfs();
    }
    return 0;
}

Comments