zzlui 1726: 迷宫【BFS】

题目描述
在很多 RPG (Role-playing Games) 游戏中,迷宫往往是非常复杂的游戏环节。通常来说,我们在走迷宫的时候都需要花非常多的时间来尝试不同的路径。但如果有了算法和计算机的帮助,我们能不能有更快的方式来解决这个问题?我们可以进行一些尝试。
现在我们有一个 N 行 M 列的迷宫。迷宫的每个格子如果是空地则可以站人,如果是障碍则不行。在一个格子上,我们可以一步移动到它相邻的 8 个空地上,但不能离开地图的边界或者跨过两个障碍的夹缝。
为了离开迷宫,我们还需要触发迷宫中所有的机关。迷宫里总共有 K 个机关,每个机关都落在一个不同的空地上。如果我们到达了某个机关所在的格子时,这个机关就会被自动触发,并在触发之后立即消失。我们的目标是按顺序触发所有的 K 个机关,而当最后一个机关被触发时,我们就可以离开迷宫了。
现在我们已经拿到了迷宫地图,并且知道所有障碍、机关的位置。初始时我们位于迷宫的某个非障碍格子上,请你计算我们最少需要移动多少步才能离开迷宫?
输入
输入的第一行是测试数据的组数 T (T ≤ 20)。
对于每组测试数据:第一行包含地图的行数 N (2 ≤ N ≤ 100),列数 M(2 ≤ M ≤ 100) 和机关的数量 K(1 ≤ K ≤10)。接下来 N 行,每行包含 M 个字符,其中字符 ‘#’ 表示障碍,而 ‘.’ 表示空地。接下来一行描述了我们的初始位置 (x, y),表示我们一开始在第 x 行第 y 列的格子上。这个格子保证是个空地。接下来 K 行,每行给出了一个机关的位置。所有的机关都不会出现在障碍上,并且任意两个机关不会出现在同一个空地上。我们需要按输入给定的顺序触发所有的 K 个机关。

输出
对于每组测试数据,输出离开迷宫所需要的最少步数。如果无论如何都不能离开迷宫,输出 -1。

样例输入
3
3 3 2



1 1
1 3
2 2
3 3 1

.#.

1 1
3 3
2 3 1
..#
.#.
1 1
2 3
样例输出
3
3
-1

分析:这类题目主要是细心,代码好写,坑点难找;
注意判断起点在机关处(在第一个机关可以,否则false),其他部分题目说的很明确了。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int MAXN = 100 + 5;
int vis[MAXN][MAXN], ma[MAXN][MAXN];
int n, m;

struct node {
    int x;
    int y;
    int step;
}e, s;

int nx[] = {1, -1, 1, -1, 0, 1, 0, -1};
int ny[] = {-1, 1, 1, -1, 1, 0, -1, 0};

bool judge(int i, int x, int y, int s1, int s2) { //仔细写 
    if(i == 0) {
        if(ma[x - 1][y] < 0 && ma[x][y + 1] < 0) return false;
        if(x == s1 && y == s2) return true;
        if(ma[x][y]) return false;
    }
    else if(i == 1) {
        if(ma[x + 1][y] < 0 && ma[x][y - 1] < 0) return false;
        if(x == s1 && y == s2) return true;
        if(ma[x][y]) return false;
    }
    else if(i == 2) {
        if(ma[x - 1][y] < 0 && ma[x][y - 1] < 0) return false;
        if(x == s1 && y == s2) return true;
        if(ma[x][y]) return false;
    }
    else if(i == 3) {
        if(ma[x + 1][y] < 0 && ma[x][y + 1] < 0) return false;
        if(x == s1 && y == s2) return true;
        if(ma[x][y]) return false;
    }
    return true; 
}

inline int spfa(int st, int ed, int s1, int s2) {
    if(st == s1 && ed == s2) return 0; //坑点 
    queue<node> que;
    memset(vis, 0, sizeof(vis));
    e.x = st, e.y = ed;
    e.step = 0;
    vis[st][ed] = 1;
    que.push(e);
    while(!que.empty()) {
        e = que.front();
        que.pop();
        for(int i = 0; i < 8; ++i) {
            int x = e.x + nx[i];
            int y = e.y + ny[i];
            if(x < 1 || y < 1 || x > n || y > m) continue;
            if(ma[x][y] < 0) continue;
            if(i < 4) {
                bool flag = judge(i, x, y, s1, s2);
                if(!flag) continue;
                if(x == s1 && y == s2) return (e.step + 1);
                s.x = x, s.y = y, s.step = e.step + 1;
                if(!vis[x][y]) que.push(s);
                vis[x][y] = 1; 
            }
            else {
                if(x == s1 && y == s2) return (e.step + 1);
                if(ma[x][y]) continue;
                s.x = x, s.y = y, s.step = e.step + 1;
                if(!vis[x][y]) que.push(s);
                vis[x][y] = 1;
            }
        }
    }
    return -1;
} 

int main() {
    int T, k;
    scanf("%d\n", &T);
    while(T--) {
        char ch;
        memset(ma, 0, sizeof(ma));
        scanf("%d %d %d", &n, &m, &k);
        for(int i = 1; i <= n; ++i) {
            getchar();
            for(int j = 1; j <= m; ++j) {
                scanf("%c", &ch);
                if(ch == '.') ma[i][j] = 0;
                else ma[i][j] = -1;
            }
        }
        int st, ed;
        int x[110], y[110];
        scanf("%d %d", &st, &ed);
        for(int i = 1; i <= k; ++i) {
            scanf("%d %d", &x[i], &y[i]);
            ma[x[i]][y[i]] = i;
        }
        x[0] = st, y[0] = ed;
        int sum = 0;
        if(ma[x[0]][y[0]] > 1) { //坑点 
            printf("-1\n");
            continue;
        }
        for(int i = 1; i <= k; ++i) {
            int cnt = spfa(x[i - 1], y[i - 1], x[i], y[i]);
            ma[x[i]][y[i]] = 0;
            if(cnt < 0) {
                sum = -1;
                break;
            }
            else sum += cnt;
        }
        printf("%d\n", sum);
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_36368339/article/details/80054541