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集,若某条竖 ...
随机推荐
- zookeeper源码(03)启动流程
本文将从启动类开始详细分析zookeeper的启动流程: 加载配置的过程 集群启动过程 单机版启动过程 启动类 org.apache.zookeeper.server.quorum.QuorumPee ...
- Julia编程基础
技术背景 Julia目前来说算是一个比较冷门的编程语言,主要是因为它所针对的应用场景实在是比较有限,Julia更注重于科学计算领域的应用.而Julia最大的特点,就是官方所宣传的:拥有C的性能,且可以 ...
- SV Interface and Program 2
Clocking:激励的时序 memory检测start信号,当start上升沿的时候,如果write信号拉高之后,将data存储到mem中 start\write\addr\data - 四个信号是 ...
- P5728 【深基5.例5】旗鼓相当的对手
1.题目介绍 2.题解 2.1 二维数组 思路 主要熟悉vector创建二维数组的方法 vector<vector> ans(N,vector(3)); 这里第一个元素表明数组大小,第二个 ...
- Linux-运行级别-init
- [转帖]Sql Server之旅——第六站 使用winHex利器加深理解数据页
https://www.cnblogs.com/huangxincheng/p/4251770.html 这篇我来介绍一个winhex利器,这个工具网上有介绍,用途大着呢,可以用来玩数据修复,恢复删除 ...
- [转帖]SIMD指令集 SSE/AVX
SIMD指令集 SSE/AVX 概述 参考手册 Intel Intrinsics Guide Tommesani.com Docs Intel 64 and IA-32 Architectures S ...
- [转帖]大模型训练,英伟达Turing、Ampere和Hopper算力分析
https://www.eet-china.com/mp/a219195.html 大 GPU 优势在于通过并行计算实现大量重复性计算.GPGPU即通用GPU,能够帮助 CPU 进行非图形相关程序的运 ...
- [转帖]超线程SMT究竟可以快多少?(AMD Ryzen版 )
https://www.modb.pro/db/139224 昨天我们用Intel I9的10核,每个核2个threads的机器跑了内核的编译: 超线程SMT究竟可以快多少? 今天,我换一台机器,采用 ...
- [转帖]Oracle入门精读28-字符集 AL32UTF8与UTF8
字符(Character) 字符是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等. 字符编码(Character Encoding) 是一套法则,使用该法则能够对自然语言的字符的一个 ...