湘潭-F: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。

示例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

題意:
給你一個迷宮,求走到出口最短時間。還是四個方向,每一步一秒,特別的就是有個傳送矩陣,能從一個點傳送到另一個點,花費三秒。

首先整個框架是個廣搜,因爲加入隊列的時間並不是遞增的(傳送要3秒,而直接走只要一秒),而我們要求最小值,就需要讓時間小的優先取出,這時候就用到優先隊列(把加入隊列的按時間小值優先)了;
放入隊列早並不代表時間短,也不代表一定會走這個點,所以放入隊列時先不標記,在取出隊列時再判斷是否用過並標記,和判斷是否是終點。

#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#define ll long long
using namespace std;
int n, m, p, v[320][320];
char ma[320][320];
struct node {
    int x, y, t;
    bool operator < (const node &a) const {
        return  t > a.t;//最小值優先
    }
} no, ne, st, en;

vector<node> ch[305][305];

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int judge(int x, int y) {
    if (x < 0 || x >= n || y < 0 || y >= m || ma[x][y] == '#')
        return 0;
    return 1;
}

int bfs() {
    priority_queue <node> q;
    memset(v, 0, sizeof(v));
    q.push(st);
    while (!q.empty()) {
        no = q.top();
        q.pop();
        if (no.x == en.x && no.y == en.y)
            return no.t;
        if (v[no.x][no.y])
            continue;
        v[no.x][no.y] = 1;
        for (int i = 0; i < 4; i++) {
            ne.x = no.x + dx[i];
            ne.y = no.y + dy[i];
            ne.t = no.t + 1;
            if (judge(ne.x, ne.y))
                q.push(ne);
        }
        for (int i = 0; i < ch[no.x][no.y].size(); i++) {
            ne = ch[no.x][no.y][i];
            ne.t = no.t + 3;
            if (judge(ne.x, ne.y))
                q.push(ne);
        }
    }
    return -1;
}

int main() {
    while (~scanf("%d%d%d", &n, &m, &p)) {
        for (int i = 0; i < 305; i++) {//清空vector
            for (int j = 0; j < 305; j++) {
                ch[i][j].clear();
            }
        }
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                scanf(" %c", &ma[i][j]);
                if (ma[i][j] == 'S')
                    st.x = i, st.y = j, st.t = 0;
                else if (ma[i][j] == 'T')
                    en.x = i, en.y = j;
            }
        int x1, x2, y1, y2;
        for (int i = 0; i < p; i++) {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            ne.x = x2, ne.y = y2;
            ch[x1][y1].push_back(ne);
        }
        printf("%d\n", bfs());
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章