# 【LeetCode】505. The Maze II 解题报告(C++)

900 篇文章 170 订阅
1033 篇文章 59 订阅

## 题目描述

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won’t stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball’s start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.

The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.

Example 1:

Input 1: a maze represented by a 2D array

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

Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4)

Output: 12

Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right.
The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.


Example 2:

Input 1: a maze represented by a 2D array

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

Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2)

Output: -1

Explanation: There is no way for the ball to stop at the destination.


Note:

1. There is only one ball and one destination in the maze.
2. Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
3. The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
4. The maze contains at least 2 empty spaces, and both the width and height of the maze won’t exceed 100.

## 解题方法

### BFS

C++代码如下：

class Solution {
public:
int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
const int M = maze.size();
const int N = maze[0].size();
vector<vector<int>> dp(M, vector<int>(N, INT_MAX));
queue<vector<int>> que;
que.push(start);
dp[start[0]][start[1]] = 0;
int count = 0;
while (!que.empty()) {
int size = que.size();
vector<int> start = que.front(); que.pop();
for (auto dir : dirs) {
vector<int> end(2, 0);
int step = rolling(maze, dir, start, end);
if (dp[end[0]][end[1]] > dp[start[0]][start[1]] + step) {
dp[end[0]][end[1]] = dp[start[0]][start[1]] + step;
que.push(end);
}
}
}
return dp[destination[0]][destination[1]] == INT_MAX ? -1 : dp[destination[0]][destination[1]];
}
private:
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int rolling(vector<vector<int>>& maze, vector<int>& curdir, vector<int>& start, vector<int>& end) {
end = start;
int step = 0;
while (true) {
int nxt_x = end[0] + curdir[0];
int nxt_y = end[1] + curdir[1];
if (nxt_x < 0 || nxt_x >= maze.size() || nxt_y < 0 || nxt_y >= maze[0].size()
|| maze[nxt_x][nxt_y] == 1) {
break;
}
end[0] = nxt_x;
end[1] = nxt_y;
step ++;
}
return step;
}
};


## 日期

2019 年 9 月 20 日 —— 是选择中国互联网式加班？还是外企式养生？

• 0
点赞
• 1
收藏
• 打赏
• 0
评论
12-30 1177
02-18 178
06-10 653
05-13
09-22 951
03-18 698
04-15 226
04-15 1790

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。