1. 题目
由空地和墙组成的迷宫中有一个球。
球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动。
当球停下时,可以选择下一个方向。
给定球的起始位置,目的地和迷宫,找出让球停在目的地的最短距离。
距离的定义是球从起始位置(不包括)到目的地(包括)经过的空地个数。
如果球无法停在目的地,返回 -1。
迷宫由一个0和1的二维数组表示。 1表示墙壁,0表示空地。
你可以假定迷宫的边缘都是墙壁。
起始位置和目的地的坐标通过行号和列号给出。
示例 1:
输入 1: 迷宫由以下二维数组表示
0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
输入 2: 起始位置坐标 (rowStart, colStart) = (0, 4)
输入 3: 目的地坐标 (rowDest, colDest) = (4, 4)
输出: 12
解析: 一条最短路径 : left -> down -> left -> down -> right -> down -> right。
总距离为 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12。
示例 2:
输入 1: 迷宫由以下二维数组表示
0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
输入 2: 起始位置坐标 (rowStart, colStart) = (0, 4)
输入 3: 目的地坐标 (rowDest, colDest) = (3, 2)
输出: -1
解析: 没有能够使球停在目的地的路径。
注意:
迷宫中只有一个球和一个目的地。
球和目的地都在空地上,且初始时它们不在同一位置。
给定的迷宫不包括边界 (如图中的红色矩形), 但你可以假设迷宫的边缘都是墙壁。
迷宫至少包括2块空地,行数和列数均不超过100。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/the-maze-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
类似题目:LeetCode 490. 迷宫(BFS/DFS)
2.1 BFS
class Solution {
public:
int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
int m = maze.size(), n = maze[0].size(), i, j, k, x, y, delta;
vector<vector<int>> dir = {{1,0},{0,1},{0,-1},{-1,0}};
queue<vector<int>> q;
vector<vector<int>> dis(m, vector<int>(n,INT_MAX));
dis[start[0]][start[1]] = 0;
q.push(start);
while(!q.empty())
{
i = q.front()[0];
j = q.front()[1];
q.pop();
for(k = 0; k < 4; ++k)
{
x = i;
y = j;
delta = 0;
while(x+dir[k][0]>=0 && x+dir[k][0]<m && y+dir[k][1]>=0 && y+dir[k][1]<n
&& maze[x+dir[k][0]][y+dir[k][1]]==0)
{
x += dir[k][0];
y += dir[k][1];
delta++;//走的步数增量
}
if(dis[i][j]+delta < dis[x][y])//该方向下一个位置为墙壁,停在xy
{
dis[x][y] = dis[i][j]+delta;
q.push({x,y});
}
}
}
return dis[destination[0]][destination[1]]==INT_MAX ? -1 : dis[destination[0]][destination[1]];
}
};
120 ms 19.7 MB
2.2 Dijkstra 最短路径
- 采用优先队列更新到某位置的最短距离
struct cmp
{
bool operator()(const vector<int>& a, const vector<int>& b) const
{
return a[2] > b[2];//距离小的在顶
}
};
class Solution {
public:
int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
int m = maze.size(), n = maze[0].size(), i, j, k, x, y, curstep, delta;
vector<vector<int>> dir = {{1,0},{0,1},{0,-1},{-1,0}};
priority_queue<vector<int>,vector<vector<int>>, cmp> q;
vector<vector<int>> dis(m, vector<int>(n,INT_MAX));
dis[start[0]][start[1]] = 0;
start.push_back(0);//第三个参数是距离
q.push(start);
while(!q.empty())
{
i = q.top()[0];
j = q.top()[1];
curstep = q.top()[2];
q.pop();
if(dis[i][j] < curstep)//距离不是最短的
continue;
for(k = 0; k < 4; ++k)
{
x = i;
y = j;
delta = 0;
while(x+dir[k][0]>=0 && x+dir[k][0]<m && y+dir[k][1]>=0 && y+dir[k][1]<n
&& maze[x+dir[k][0]][y+dir[k][1]]==0)
{
x += dir[k][0];
y += dir[k][1];
delta++;//走的步数增量
}
if(dis[i][j]+delta < dis[x][y])//该方向下一个位置为墙壁,停在这
{
dis[x][y] = dis[i][j]+delta;
q.push({x,y,dis[x][y]});
}
}
}
return dis[destination[0]][destination[1]]==INT_MAX ? -1 : dis[destination[0]][destination[1]];
}
};
124 ms 18.3 MB
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!