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集,若某条竖 ...
随机推荐
- Guava缓存工具类封装和使用
本文为博主原创,未经允许不得转载: Guava是谷歌提供的一款强大的java工具库,里面包含了很多方便且高效的工具,在项目开发中有业务场景需要保存数据到内存当中, 且只需要保存固定时间就可以,该数据只 ...
- 【Spring 5核心原理】1设计模式
1.1开闭原则 开闭原则(open-closed principle,OCP)是指一个软件实体(如类,模块和函数)应该对扩展开放,对修改关闭.所谓的开闭,也正是对扩展和修改两个行为的一个原则. 强调用 ...
- Qt5.9 UI设计(五)——将Tabwidget与treeWidget相互关联
前言 前面一章介绍了ControlTabWidget ControlTreeWidget maintitlebar 三个子页面同时布局到 mainwindow 的方法,本章介绍如何将ControlTr ...
- [转帖]SQL Server索引的维护 - 索引碎片、填充因子
https://www.cnblogs.com/kissdodog/archive/2013/06/14/3135412.html 这两个问题都和页密度有关,虽然两者的表现形式在本质上有所区别,但是故 ...
- drop_caches 的简单学习
drop_caches 的简单学习 背景 最近一段时间一直在学习内存相关的知识 Linux系统里面的内存管理还是非常复杂的. 我这边理解 Linux从宏观层次的 段页式内存管理 到细节的buddy和s ...
- [转帖]Difference between localhost and 127.0.0.1?
https://www.tutorialspoint.com/difference-between-localhost-and-127-0-0-1#:~:text=The%20most%20signi ...
- [转帖]在yum安装本地rpm文件时遇到public key不正确问题
yum错误:public.gpg.key: import read failed(2). 在yum安装本地rpm文件时遇到public key不正确问题 Downloading Packages: ...
- [转帖]5、kafka监控工具Kafka-Eagle介绍及使用
https://zhuanlan.zhihu.com/p/628039102 # Apache Kafka系列文章 1.kafka(2.12-3.0.0)介绍.部署及验证.基准测试 2.java调 ...
- [转帖]Kafka高可用 — KRaft集群搭建
Apache Kafka Raft 是一种共识协议,它的引入是为了消除 Kafka 对 ZooKeeper 的元数据管理的依赖,被社区称之为 Kafka Raft metadata mode,简称 K ...
- [转帖]docker 最新版本升级
文章目录 前言 一.卸载低版本docker 1.1 检查docker版本 1.2 删除docker 二.开始安装 2.1 安装所需依赖 2.2 设置docker yum源 2.3 查看所有可用版本 2 ...