2018湘潭校赛 F-maze ( BFS

maze

题目描述

链接:https://www.nowcoder.com/acm/contest/105/F
来源:牛客网

小明来到一个由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。

样例

输入
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

题意

BFS的模板,不过队列里面要用堆进行优化,才能找到最小的

AC代码

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXN = 300+10;
int n, m, q;
bool vis[MAXN][MAXN];
char mps[MAXN][MAXN];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int sx, sy;
struct node {
    int x, y, st;
    node(int _x=0, int _y=0, int _st=0):x(_x),y(_y),st(_st){}
    bool operator<(const node &r) const {
        return st > r.st;  // 小顶堆
    }
};
vector<node> v[MAXN][MAXN];

bool check(int x, int y) {
    return x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==false&&mps[x][y]!='#';
}
priority_queue<node> que;
int bfs() {
    while(!que.empty()) que.pop();
    CLR(vis,false);
    que.push(node(sx,sy,0));
    node tmp;
    while(!que.empty()) {
        tmp = que.top();
        que.pop();
        if(mps[tmp.x][tmp.y] == 'T')
            return tmp.st;
        if(vis[tmp.x][tmp.y])
            continue;
        vis[tmp.x][tmp.y] = true;
        for(int i = 0; i < 4; i++) {
            int xx = tmp.x+dx[i];
            int yy = tmp.y+dy[i];
            int zz = tmp.st+1;
            if(check(xx,yy)) {
                que.push(node(xx,yy,zz));
            }
        }
        for(int i = 0; i < v[tmp.x][tmp.y].size(); i++) {
            node ss = v[tmp.x][tmp.y][i];
            ss.st = tmp.st+3;
            if(check(ss.x, ss.y))
                que.push(ss);
        }
    }
    return -1;
}
int main() {
    ios::sync_with_stdio(false);
    while(cin >> n >> m >> q) {
        CLR(mps,0);
        for(int i = 0; i < MAXN; i++)
            for(int j = 0; j < MAXN; j++)
                v[i][j].clear();
        for(int i = 0; i < n; i++)
            cin >> mps[i];
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(mps[i][j] == 'S')
                    sx=i, sy=j;
            }
        }
        for(int i = 0; i < q; i++) {
            int x1, x2, y1, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if(mps[x1][y1]!='#' && mps[x2][y2]!='#') {
                v[x1][y1].push_back(node(x2,y2,0));
            }
        }
        cout << bfs() << endl;
    }
return 0;
}

/*
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
*/

猜你喜欢

转载自blog.csdn.net/wang2332/article/details/80174289