NC15665 maze
题目
题目描述
小明来到一个由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的更多相关文章
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- (期望)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 ...
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- uva705--slash maze
/*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...
- 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 ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- HDU 4035:Maze(概率DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description When w ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- netty系列之:netty中的自动解码器ReplayingDecoder
目录 简介 ByteToMessageDecoder可能遇到的问题 ReplayingDecoder的实现原理 总结 简介 netty提供了一个从ByteBuf到用户自定义的message的解码器叫做 ...
- 【论文笔记】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 ...
- MySQL性能优化 - 别再只会说加索引了
MySQL性能优化 MySQL性能优化我们可以从以下四个维度考虑:硬件升级.系统配置.表结构设计.SQL语句和索引. 从成本上来说:硬件升级>系统配置>表结构设计>SQL语句及索引, ...
- Oracle查看表空间大小
遇到报错 java.sql.SQLException: ORA-01653: 表 MESHIS.HIS_RET_LOT_FQC 无法通过 8 (在表空间 MESHIS_DATA_TBS 中) 扩展 a ...
- java.time包 时间处理类
已经习惯用 Date类这里就不再赘述,下面介绍新的时间处理类 1.LocalDate类 // 本地日期LocalDate localDate = LocalDate.of(2022, 2, 27);S ...
- PowerJob高级特效-容器部署完整教程
介绍 powerjob提供了容器功能,用来做一些灵活的任务处理.这里容器为 JVM 级容器,而不是操作系统级容器(Docker).(至于为什么取"容器"这个有歧义的名字是因为作者没 ...
- vue生命周期加载顺序
1.beforeCreate(创建前)表示实例完全被创建出来之前,vue 实例的挂载元素$el和数据对象 data 都为 undefined,还未初始化.此钩子函数不能获取到数据,dom元素也没有渲染 ...
- layui数据表格搜索
简单介绍 我是通过Servlet传递json给layui数据表格模块,实现遍历操作的,不过数据量大的话还是需要搜索功能的.这是我参考网上大佬代码写出的搜索功能. 实现原理 要实现搜索功能,肯定需要链接 ...
- Android——RelativeLayout
代码:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android= ...
- ML第3周学习小结
本周收获 总结一下本周学习内容: 1.学习了<深入浅出Pandas>的第五章:Pandas高级操作的三个内容 复杂查询 数据类型转换 数据排序 我的博客链接: Pandas复杂查询.数据类 ...