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;
}
NC24605 [USACO 2011 Ope S]Corn Maze的更多相关文章
- 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 ...
- 3299: [USACO2011 Open]Corn Maze玉米迷宫
3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 137 Solved: 59[ ...
- 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 ...
- 洛谷——P1825 [USACO11OPEN]玉米田迷宫Corn Maze
P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...
- 洛谷—— P1825 [USACO11OPEN]玉米田迷宫Corn Maze
https://www.luogu.org/problem/show?pid=1825 题目描述 This past fall, Farmer John took the cows to visit ...
- 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze
P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...
- USACO 2006 November Gold Corn Fields
USACO 2006 November Gold Corn Fields 题目描述: Farmer John has purchased a lush new rectangular pasture ...
- [USACO11OPEN]玉米田迷宫Corn Maze
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
- NC25136 [USACO 2006 Ope B]Cows on a Leash
NC25136 [USACO 2006 Ope B]Cows on a Leash 题目 题目描述 给定如图所示的若干个长条.你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其 ...
- [USACO 2011 Nov Gold] Cow Steeplechase【二分图】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...
随机推荐
- 08-避免Latch的产生
1.Latch简介 Latch就是锁存器,是一种在异步电路系统中,对输入信号电平敏感的单元,用来存储信息 锁存器在数据未锁存时,输出端的信号随输入信号变化,就像信号通过一个缓冲器,一旦锁存信号有效,数 ...
- 【CubeMX】使用 CubeMX 生成对应的配置代码需要设置 “User Label”
如要生成 SPI 的管脚配置代码,需要设置 User Label,这样工具才能知道应该配置什么,否则不会生成
- 2023浙江省大学生信息安全竞赛技能赛初赛 部分wp
CRYPTO 小小数学家 1.题目信息 查看代码 19+49=? 96-31=? 86-3=? 20+47=? 29+55=? 35+35=? 81+42=? 73-16=? 52+48=? 0+56 ...
- java - 正确关闭流
package stream; import java.io.*; public class FileReaderTest { public static void main(String[] arg ...
- Redis-主从复制-哨兵模式
- [转帖]Springboot配置kafka用户名密码
华为云开发者联盟 Springboot配置kafka用户名密码 Springboot配置kafka用户名密码 SpringBoot配置kafka用户名密码 Springboot配置kafka用户名密码 ...
- 【转帖】QUIC协议简史
QUIC简史 QUIC(Quick UDP Internet Connection)是谷歌推出的一套基于UDP的传输协议,它实现了TCP + HTTPS + HTTP/2的功能,目的是保证可靠性的同时 ...
- [转帖]Centos 7 查看磁盘io ,找出占用io读写很高的进程
1,先用iostat查看磁盘io 是否读写负载很高 用iostat -x 1 10 如果 iostat 没有,要 yum install sysstat安装这个包,第一眼看下图红色圈圈的那个如果%ut ...
- [转帖]龙芯3A5000评测 国产自主指令集架构实战
https://tieba.baidu.com/p/8297036384?pid=147031768904&cid=#147031768904 芯片,是世界一大难题,很多人难以想象电子硬件 ...
- [转帖]Linux性能调优之内存负载调优的一些笔记
https://zhuanlan.zhihu.com/p/548770928 写在前面 整理一些Linux内存调优的笔记,分享给小伙伴 博文没有涉及的Demo,理论方法偏多,可以用作内存调优入门 博文 ...