NC24605 [USACO 2011 Ope S]Corn Maze

题目链接

题目

题目描述

This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides work in both directions: a cow can slide from the slide's start to the end instantly, or from the end to the start. If a cow steps on a space that hosts either end of a slide, she must use the slide.
The outside of the corn maze is entirely corn except for a single exit.
The maze can be represented by an N x M (2 <= N <= 300; 2 <= M <= 300) grid. Each grid element contains one of these items:
* Corn (corn grid elements are impassable)
* Grass (easy to pass through!)
* A slide endpoint (which will transport a cow to the other endpoint)
* The exit
A cow can only move from one space to the next if they are adjacent and neither contains corn. Each grassy space has four potential neighbors to which a cow can travel. It takes 1 unit of time to move from a grassy space to an adjacent space; it takes 0 units of time to move from one slide endpoint to the other.
Corn-filled spaces are denoted with an octothorpe (#). Grassy spaces are denoted with a period (.). Pairs of slide endpoints are denoted with the same uppercase letter (A-Z), and no two different slides have endpoints denoted with the same letter. The exit is denoted with the equals sign (=).
Bessie got lost. She knows where she is on the grid, and marked her current grassy space with the 'at' symbol (@). What is the minimum time she needs to move to the exit space?

输入描述

  • Line 1: N M
  • Lines 2..N+1: Line i+1 describes the Line i of the maze

输出描述

  • Line 1: A single integer, the minimum time she needs to move to the exit space.

示例1

输入

5 6
###=##
#.W.##
#.####
#.@W##
######

输出

3

题解

知识点:BFS。

又是一道传送门的题,显然用bfs搜索最短路。但传送是立刻的,可以理解为走上去立刻传送,整个过程步数为 \(1\) ,因此不需要维护时间线,只要每次扩展特判传送门就行。

传送门标记有点烦,用字母作为下标,存储传送的两个点坐标,如果踩到字母,那就传送的不是当前坐标的坐标即可。

时间复杂度 \(O(?)\)

空间复杂度 \(O(nm)\)

代码

#include <bits/stdc++.h>

using namespace std;

int n, m;
char dt[307][307];
bool vis[307][307];
const int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
struct node {
    int x, y, step;
};
vector<node> tsm[30];

int bfs(node st) {
    queue<node> q;
    q.push(st);
    vis[st.x][st.y] = 1;
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        if (dt[cur.x][cur.y] == '=') 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 || dt[xx][yy] == '#' || vis[xx][yy]) continue;
            vis[xx][yy] = 1;
            if (dt[xx][yy] >= 'A' && dt[xx][yy] <= 'Z') {
                for (auto it : tsm[dt[xx][yy] - 'A']) {
                    if (it.x != xx || it.y != yy) {
                        xx = it.x;
                        yy = it.y;
                        break;
                    }
                }
            }
            q.push({ xx,yy,cur.step + 1 });
        }
    }
    return -1;
}



int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    node st;
    for (int i = 0;i < n;i++) {
        for (int j = 0;j < m;j++) {
            cin >> dt[i][j];
            if (dt[i][j] == '@') st = { i,j,0 };
            if (dt[i][j] >= 'A' && dt[i][j] <= 'Z')
                tsm[dt[i][j] - 'A'].push_back({ i,j,0 });
        }
    }
    cout << bfs(st) << '\n';

    return 0;
}
posted @ 2022-07-16 19:51  空白菌  阅读(64)  评论(0)    收藏  举报