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集,若某条竖 ...
随机推荐
- vscode的配置文件
vscode的配置文件 总述:vscode中一般会在项目文件夹下自动生成.vscode文件夹,其中存放若干配置文件(.json),一般有如下文件: 下面将解释每个文件的用途与表现. 1. c_cpp_ ...
- [转帖]记druid 连接池没满,但超时问题 GetConnectionTimeoutException active 5, maxActive 100
记druid 连接池没满,但超时问题 GetConnectionTimeoutException active 5, maxActive 100 问题说明 线上服务突然出现报错,通过日志查找发现是因为 ...
- [转帖]缓存与存储的一致性策略:从 CPU 到分布式系统
https://zhuanlan.zhihu.com/p/151745863 在计算机系统设计实践中,我们常常会遇到下图所示架构: 为了解决单个存储器读吞吐无法满足要求的问题,常常需要在存储器上面增加 ...
- [转帖]【有效解决】Edge浏览器提示你的连接不是专用连接怎么办?
https://www.xitongzhijia.net/xtjc/20230524/290887.html Win11正式版iso镜像最新(22H2新版) V2023 大小:4.22 GB类别:Wi ...
- [转帖]怎么查看Linux服务器硬件信息,这些命令告诉你
https://zhuanlan.zhihu.com/p/144368206 Linux服务器配置文档找不到,你还在为查询Linux服务器硬件信息发愁吗?学会这些命令,让你轻松查看Linux服务器的C ...
- killall 以及 pkill 等命令
https://zhidao.baidu.com/question/1500084252693125099.html // 通过 killall 命令killall nginx// 通过 pkill ...
- 阿里云ECS虚拟机磁盘扩容过程
阿里云ECS虚拟机磁盘扩容过程 背景 公司同事将很早之前的一个虚拟机重新开机. 就好将一套demo环境安装进这个ECS虚拟机里面 这个机器系统盘只有40G的空间. 导致磁盘空间不足. 其实一开始我不知 ...
- 解锁前端新潜能:如何使用 Rust 锈化前端工具链
前言 近年来,Rust的受欢迎程度不断上升.首先,在操作系统领域,Rust 已成为 Linux 内核官方认可的开发语言之一,Windows 也宣布将使用 Rust 来重写内核,并重写部分驱动程序. ...
- uni-app中使用map
uni-app中使用地图显示当前的位置 我们现在的需求是,显示用户在地图上所处的位置. 有的小伙伴可能会说,这个是不是需要接入第三方的地图. 其实是不需要的,从目前这个需求来看. 我们只需要引入uni ...
- vue3动态组件的展示
需求描述 有些时候,我们需要做这样的处理. 点击A按钮的时候,出现组件A 点击B按钮的时候,出现组件B 点击C按钮的时候,出现组件C 这个时候,我们就可以使用动态组件了 动态组件 <templa ...