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 ...
随机推荐
- rabbitmq 安装延时队列插件rabbitmq-delayed-message-exchange
1.下载rabbitmq-delayed-message-exchange(注意版本对应) 链接:https://github.com/rabbitmq/rabbitmq-delayed-messag ...
- 斯坦福NLP课程 | 第1讲 - NLP介绍与词向量初步
作者:韩信子@ShowMeAI,路遥@ShowMeAI,奇异果@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www. ...
- 解决学校在线评测系统不支持C++11的问题
如图,我们老师搞的这个评测系统它不支持C++11.但分析错误信息可知GCC本身版本是支持C++11的,只不过没开开.平时的时候我们可以对"g++"后使用"-std=c++ ...
- Android 图像显示系统 - 导出图层数据的方法介绍(dump GraphicBuffer raw data)
一.前言 在项目的开发中,为了定位Android显示异常的原因:GPU渲染 or GPU合成 or HWC合成送显异常的问题.我们通常会把图层的原始数据写到文件,然后通过RGB或YUV的软件工具来查看 ...
- 实用git指令
实用git指令 clone 操作目的 简要操作说明 详细操作说明 init & add & commit & push 操作目的 简要操作说明 详细操作说明 pull & ...
- Vue项目中的接口进阶使用
创建services文件夹 1.文件夹apis.index.request的三个文件. 2.apis文件放接口 export const apis = { checkDeviceNo: '/api/c ...
- 降维、特征提取与流形学习--非负矩阵分解(NMF)
非负矩阵分解(NMF)是一种无监督学习算法,目的在于提取有用的特征(可以识别出组合成数据的原始分量),也可以用于降维,通常不用于对数据进行重建或者编码. NMF将每个数据点写成一些分量的加权求和(与P ...
- transforms.py
from PIL import Image from torchvision import transforms #python的用法-->tensor数据类型 #通过transforms.To ...
- VmWare安装Centos8注意事项
VmWare安装Centos8注意事项 1.需选择稍后安装操作系统 2.选择操作系统版本 3.修改虚拟机配置 4.配置完成点击开启虚拟机(注意要将鼠标放在屏幕中央,点击一下后才能使用上下键进行选择) ...
- 基于SqlSugar的开发框架循序渐进介绍(7)-- 在文件上传模块中采用选项模式【Options】处理常规上传和FTP文件上传
在基于SqlSugar的开发框架的服务层中处理文件上传的时候,我们一般有两种处理方式,一种是常规的把文件存储在本地文件系统中,一种是通过FTP方式存储到指定的FTP服务器上.这种处理应该由程序进行配置 ...