题目链接

题目

题目描述

小明来到一个由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. 【CSAPP】Bomb Lab实验笔记

    bomblab这节搞的是二进制拆弹,可以通俗理解为利用反汇编知识找出程序的六个解锁密码. 早就听闻BOMBLAB的大名,再加上我一直觉得反汇编是个很艰难的工作,开工前我做好了打BOSS心理准备.实际上 ...

  2. 【论文笔记】A review of applications in federated learning(综述)

    A review of applications in federated learning Authors Li Li, Yuxi Fan, Mike Tse, Kuo-Yi Lin Keyword ...

  3. 1.sprng 简介

    容器(可以用来管理所有的组件(类)) 核心关注:IOC和AOP 1.IOC Inversion(反转) Of Control:控制反转 控制:资源的获取方式 1.主动式(要什么资源自己创建) Pers ...

  4. maven install resources failed: newPosition < 0: (-1 < 0)

    添加以下代码在 pom.xml 中,具体参阅这里 <build> <plugins> <plugin> <groupId>org.apache.mave ...

  5. Spring Boot 3.0.0 M3、2.7.0发布,2.5.x将停止维护

    昨晚(5月19日),Spring Boot官方发布了一系列Spring Boot的版本更新,其中包括: Spring Boot 3.0.0-M3 Spring Boot 2.7.0 Spring Bo ...

  6. linux下三种服务开机自启的方式

    方式一.二.三适用于ubuntu,centos推荐使用方式二.方式三 方式一 在ubuntu系统中,如果你使用的apt方式安装的软件,可以使用如下方式直接添加服务的开机自启, 如果你是手动解压缩官网下 ...

  7. linux篇-linux命令大全

    Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短 ...

  8. CenterNet和CenterNet2笔记

    CenterNet和CenterNet2笔记 CenterNet是基于anchor-free的一阶段检测算法 CenterNet2是CenterNet作者基于两阶段的改进 CenterNet(Obje ...

  9. 《Effective C++》阅读总结(三):资源管理

    C++中的资源管理非常重要,在将资源加载到内存后,便可以使用资源了,当我们不再需要资源时,我们要保证其正确的释放,才能将其占用的内存空间归还给操作系统,不正确的释放很容易造成内存泄漏.本章以资源管理类 ...

  10. 用Arduino显示颜色序列(u8g2,OLED)

    目录 用Arduino显示颜色序列(u8g2,OLED) 用Arduino显示颜色序列(u8g2,OLED) 提前祝大家新年快乐! 主控:Arduino Mega 2560 硬件:126×64 OLE ...