题目链接

题目

题目描述

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;
}

NC24605 [USACO 2011 Ope S]Corn Maze的更多相关文章

  1. Alberta family's QR code is world's largest corn maze

    BY DARREN WEIR     SEP 10, 2012 IN ODD NEWS Link:http://www.digitaljournal.com/article/332512   Laco ...

  2. 3299: [USACO2011 Open]Corn Maze玉米迷宫

    3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 59[ ...

  3. P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  4. 洛谷——P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  5. 洛谷—— P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    https://www.luogu.org/problem/show?pid=1825 题目描述 This past fall, Farmer John took the cows to visit ...

  6. 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  7. USACO 2006 November Gold Corn Fields

    USACO 2006 November Gold Corn Fields 题目描述: Farmer John has purchased a lush new rectangular pasture ...

  8. [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  9. NC25136 [USACO 2006 Ope B]Cows on a Leash

    NC25136 [USACO 2006 Ope B]Cows on a Leash 题目 题目描述 给定如图所示的若干个长条.你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其 ...

  10. [USACO 2011 Nov Gold] Cow Steeplechase【二分图】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...

随机推荐

  1. python pip手动安装二进制包

    python中使用pip安装扩展包的时候,有时候会遇到如下类似报错: Running setup.py install for mysqlclient ... error ...(中间报错信息省略) ...

  2. 【南大静态代码分析】作业 2:常量传播和 Worklist 求解器

    作业 2:常量传播和 Worklist 求解器 题目链接:https://tai-e.pascal-lab.net/pa2.html 评测链接:https://oj.pascal-lab.net/pr ...

  3. python毕业设计选题15例,马上要毕业啦,大家做好准备了没

    Hi,大家好,大四的同学马上要开始毕业设计啦,大家做好准备了没! 学长给大家详细整理了最新的python计算机毕设相关选题,对选题有任何疑问,都可以问学长哦. 1. 网上商城系统 这是一个基于pyth ...

  4. SpringBoot01:HelloWorld!

    回顾Spring Spring是一个开源框架,2003年兴起的一个轻量级的Java开发框架. Spring是为了解决企业级应用开发的复杂性而创建的,简化开发. Spring是怎样简化Java开发的呢? ...

  5. [转帖]k8s ipv4/ipv6双栈实践

    https://www.iceyao.com.cn/post/2020-11-28-k8s_dual_stack/ Posted by 爱折腾的工程师 on Saturday, November 28 ...

  6. [转帖]性能优化:Swap调优

    目标:解决大量Log写入占用大量的File Cache,内容利用不充分导致swap 基本原则:尽量使用内存,减少swap,同时,尽早flush到外存,早点释放内存给写cache使用.---特别在持续的 ...

  7. [转帖]解决jmeter请求响应结果乱码的问题

    如下图所示,请求百度接口的时候,发现返回的信息里面中文是乱码 这个时候我们只需要改一下jmeter里的配置文件,设置响应结果的字符编码为UTF-8就行了. 进入jmeter安装目录/bin中,找到jm ...

  8. [转帖]VMWare ESXi中,不同的虚拟网卡性能竟然能相差三倍!

    https://zhuanlan.zhihu.com/p/525656364 正文共:1024 字 11 图,预估阅读时间:1 分钟 在上个实验中(VPP使用DPDK纳管主机网卡),我们已经初步实现了 ...

  9. echarts设置标题样式

    <!DOCTYPE html> <html> <!-- https://blog.csdn.net/weixin_42698255/article/details/892 ...

  10. Python 潮流周刊第 37 期(摘要)

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...