题目链接

题目

题目描述

小明来到一个由n x m个格子组成的迷宫,有些格子是陷阱,用'#'表示,小明进入陷阱就会死亡,'.'表示没有陷阱。小明所在的位置用'S'表示,目的地用'T'表示。

小明只能向上下左右相邻的格子移动,每移动一次花费1秒。

有q个单向传送阵,每个传送阵各有一个入口和一个出口,入口和出口都在迷宫的格子里,当走到或被传送到一个有传送阵入口的格子时,小明可以选择是否开启传送阵。如果开启传送阵,小明就会被传送到出口对应的格子里,这个过程会花费3秒;如果不开启传送阵,将不会发生任何事情,小明可以继续向上下左右四个方向移动。

一个格子可能既有多个入口,又有多个出口,小明可以选择任意一个入口开启传送阵。使用传送阵是非常危险的,因为有的传送阵的出口在陷阱里,如果小明使用这样的传送阵,那他就会死亡。也有一些传送阵的入口在陷阱里,这样的传送阵是没有用的,因为小明不能活着进入。请告诉小明活着到达目的地的最短时间。

输入描述

有多组数据。对于每组数据:

第一行有三个整数n,m,q(2≤ n,m≤300,0≤ q ≤ 1000)。

接下来是一个n行m列的矩阵,表示迷宫。

最后q行,每行四个整数x1,y1,x2,y2(0≤ x1,x2< n,0≤ y1,y2< m),表示一个传送阵的入口在x1行y1列,出口在x2行y2列。

输出描述

如果小明能够活着到达目的地,则输出最短时间,否则输出-1。

示例1

输入

5 5 1
..S..
.....
.###.
.....
..T..
1 2 3 3
5 5 1
..S..
.....
.###.
.....
..T..
3 3 1 2
5 5 1
S.#..
..#..
###..
.....
....T
0 1 0 2
4 4 2
S#.T
.#.#
.#.#
.#.#
0 0 0 3
2 0 2 2

输出

6
8
-1
3

备注

坐标从0开始

题解

方法一

知识点:BFS,优先队列。

显然用bfs,但要做修正的是把队列更换为优先队列,因为传送门的存在使得步数时间线被破坏,先到的点不一定步数比后到的点少,因此优先队列维护步数从小到大扩展。但就不能每次扩展直接锁点了,要在每个点真正经过的时候才考虑是否锁点,如果被之前的经过了,则跳过。

细节上注意传送门的存取,用入口作为下标,多个出口用 vector 存储,形成一个 vector 的二维数组,可以方便扩展。

遇到出口直接跳出即可,因为优先队列维护了时间。

时间复杂度 \(O(?)\)

空间复杂度 \(O(mn)\)

方法二

知识点:BFS。

如果不用优先队列也可以做,每次扩展如果扩展的点时间更小不扩展,否则覆盖时间。与优先队列的区别在于,这种做法要遍历地图才可以得到结果。

时间复杂度 \(O(?)\)

空间复杂度 \(O(mn)\)

代码

方法一

#include <bits/stdc++.h>

using namespace std;

int n, m, q;
char dt[307][307];
bool vis[307][307];
const int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
vector<pair<int, int>> tsm[307][307]; struct node {
int x, y;
int step;
friend bool operator<(const node &a, const node &b) {
return a.step > b.step;
}
}; int bfs(node init) {
priority_queue<node> pq;
pq.push(init);
while (!pq.empty()) {
node cur = pq.top();
pq.pop();
if (vis[cur.x][cur.y]) continue;///但一定是最短的时候经过
vis[cur.x][cur.y] = 1;
if (dt[cur.x][cur.y] == 'T') 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 || vis[xx][yy] || dt[xx][yy] == '#') continue;
pq.push({ xx,yy,cur.step + 1 });///扩展出来的点不一定是最短的,不能在这里vis
}
for (auto tr : tsm[cur.x][cur.y]) {
if (vis[tr.first][tr.second] || dt[tr.first][tr.second] == '#') continue;
pq.push({ tr.first,tr.second,cur.step + 3 });
}
}
return -1;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
while (cin >> n >> m >> q) {
memset(vis, 0, sizeof(vis));
node init;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
cin >> dt[i][j];
if (dt[i][j] == 'S') init.x = i, init.y = j, init.step = 0;
}
}
for (int i = 0;i < q;i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
tsm[x1][y1].push_back({ x2,y2 });
}
cout << bfs(init) << '\n';
for (int i = 0;i < n;i++)
for (int j = 0;j < m;j++)
tsm[i][j].clear();
}
return 0;
}

方法二

#include <bits/stdc++.h>

using namespace std;

int n, m, q;
char dt[307][307];
int vis[307][307];///记录步数,用小的替换大的
const int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
vector<pair<int, int>> tsm[307][307]; struct node {
int x, y;
}; void bfs(node init) {
queue<node> q;
q.push(init);
vis[init.x][init.y] = 0;
int ans = -1;
while (!q.empty()) {
node cur = q.front();
q.pop();
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 || vis[xx][yy] <= vis[cur.x][cur.y] + 1 || dt[xx][yy] == '#') continue;
q.push({ xx,yy });
vis[xx][yy] = vis[cur.x][cur.y] + 1;
}
for (auto tr : tsm[cur.x][cur.y]) {
if (vis[tr.first][tr.second] <= vis[cur.x][cur.y] + 3 || dt[tr.first][tr.second] == '#') continue;
q.push({ tr.first,tr.second });
vis[tr.first][tr.second] = vis[cur.x][cur.y] + 3;
}
}
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
while (cin >> n >> m >> q) {
memset(vis, 0x3f, sizeof(vis));
node init, ans;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
cin >> dt[i][j];
if (dt[i][j] == 'S') init.x = i, init.y = j;
if (dt[i][j] == 'T') ans.x = i, ans.y = j;
}
}
for (int i = 0;i < q;i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
tsm[x1][y1].push_back({ x2,y2 });
}
bfs(init);
cout << (vis[ans.x][ans.y] > 3e5 ? -1 : vis[ans.x][ans.y]) << '\n';
for (int i = 0;i < n;i++)
for (int j = 0;j < m;j++)
tsm[i][j].clear();
}
return 0;
}

NC15665 maze的更多相关文章

  1. Backtracking algorithm: rat in maze

    Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...

  2. (期望)A Dangerous Maze(Light OJ 1027)

    http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...

  3. 1204. Maze Traversal

    1204.   Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...

  4. uva705--slash maze

    /*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...

  5. HDU 4048 Zhuge Liang's Stone Sentinel Maze

    Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/327 ...

  6. Borg Maze(MST & bfs)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Descrip ...

  7. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  8. HDU 4035:Maze(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description   When w ...

  9. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

随机推荐

  1. 论文解读(SAGPool)《Self-Attention Graph Pooling》

    论文信息 论文标题:Self-Attention Graph Pooling论文作者:Junhyun Lee, Inyeop Lee, Jaewoo Kang论文来源:2019, ICML论文地址:d ...

  2. Python版本共存、语法、变量和数据类型

    python多版本共存 主要是把两个版本的python解释器的所在路径都加入环境变量当中,之后重新命名python解释器文件名称就好 先拷贝一个启动程序,在进行改名就好 python.exe pyth ...

  3. JS运算符,流程控制,函数,内置对象,BOM与DOM

    运算符 1.算数运算符 运算符 描述 + 加 - 减 * 乘 / 除 % 取余(保留整数) ++ 递加 - - 递减 ** 幂 var x=10; var res1=x++; '先赋值后自增1' 10 ...

  4. django基础--02基于数据库的小项目

    摘要:简单修改.增加部分页面,了解django开发的过程.(Python 3.9.12,django 4.0.4 ) 接前篇,通过命令: django-admin startproject myWeb ...

  5. C# WPF后台动态添加控件(经典)

    概述 在Winform中从后台添加控件相对比较容易,但是在WPF中,我们知道界面是通过XAML编写的,如何把后台写好的控件动态添加到前台呢?本节举例介绍这个问题. 这里要用到UniformGrid布局 ...

  6. OpenHarmony3.1 Release版本关键特性解析——Enhanced SWAP内存管理

    樊成阳 华为技术有限公司内核专家 陈杰 华为技术有限公司内核专家 OpenAtom OpenHarmony(以下简称"OpenHarmony")是面向全场景泛终端设备的操作系统,终 ...

  7. supervisor安装以及监控管理rabbitmq消费者进程

    简介:Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启. 1.安装 apt-get install ...

  8. ElasticSearch基础学习(SpringBoot集成ES)

    一.概述 什么是ElasticSearch? ElasticSearch,简称为ES, ES是一个开源的高扩展的分布式全文搜索引擎. 它可以近乎实时的存储.检索数据:本身扩展性很好,可以扩展到上百台服 ...

  9. unity---脚本代码报错

    同版本编辑器在不同VS版本下会报错问题解决 解决方法 报错情况 解决方法 打开Unity --->Preferences--->External Tools然后点击:Regenerate ...

  10. Vue 中 watch 的一个坑

    开发所用 Vue 版本 2.6.11 子组件 coma 中两个属性: props: { url: { type: String, default: '' }, oriurl:{ type: Strin ...