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
Example 1:
Input:
(rowStart, colStart) = (0,4)
(rowDest, colDest)= (4,4)
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
Output: 12
Explanation:
(0,4)->(0,3)->(1,3)->(1,2)->(1,1)->(1,0)->(2,0)->(2,1)->(2,2)->(3,2)->(4,2)->(4,3)->(4,4)
Example 2:
Input:
(rowStart, colStart) = (0,4)
(rowDest, colDest)= (0,0)
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
Output: 6
Explanation:
(0,4)->(0,3)->(1,3)->(1,2)->(1,1)->(1,0)->(0,0)
Notice
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.
分析
这道题与Maze I 题目很类似,不同点在于这道题需要计算到目的地的最短距离。BFS的算法保持不变,记录距离时我们可以使用一个二维数组dp记录对应坐标的距离。在做BFS时,如果此时得到[x][y]的点,那么分别向上下左右找到靠墙的点,计算[x][y]点到该靠墙点的距离 + dp[x][y]是否比当前的值小,如果小,则将该靠墙的点加入队列中。
Code
class Solution {
public:
/**
* @param maze: the maze
* @param start: the start
* @param destination: the destination
* @return: the shortest distance for the ball to stop at the destination
*/
int shortestDistance(vector<vector<int>> &maze, vector<int> &start, vector<int> &destination) {
// write your code here
int rows = maze.size();
if (rows == 0)
return false;
int cols = maze[0].size();
maze.insert(maze.begin(), vector<int>(cols, 1));
maze.push_back(vector<int>(cols, 1));
rows += 2;
for (int i = 0; i < rows; i ++)
{
maze[i].insert(maze[i].begin(), 1);
maze[i].push_back(1);
}
cols += 2;
vector<vector<int>> dp(rows, vector<int>(cols, INT_MAX));
int sx = start[0] + 1;
int sy = start[1] + 1;
int dx = destination[0] + 1;
int dy = destination[1] + 1;
dp[sx][sy] = 0;
queue<pair<int, int>> q;
q.push(make_pair(sx, sy));
return findRoute(maze, q, dp, dx, dy);
}
int findRoute(vector<vector<int>>& maze, queue<pair<int, int>> q, vector<vector<int>>& dp, int dx, int dy)
{
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
int tmpx = x;
int tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpx --;
}
if (dp[tmpx+1][tmpy] > dp[x][y] + (x-tmpx-1))
{
dp[tmpx+1][tmpy] = dp[x][y] + (x-tmpx-1);
q.push(make_pair(tmpx+1, tmpy));
}
tmpx = x;
tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpx ++;
}
if (dp[tmpx-1][tmpy] > dp[x][y] + (tmpx-1-x))
{
dp[tmpx-1][tmpy] = dp[x][y] + (tmpx-1-x);
q.push(make_pair(tmpx-1, tmpy));
}
tmpx = x;
tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpy --;
}
if (dp[tmpx][tmpy+1] > dp[x][y] + (y-tmpy-1))
{
dp[tmpx][tmpy+1] = dp[x][y] + (y-tmpy-1);
q.push(make_pair(tmpx, tmpy+1));
}
tmpx = x;
tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpy ++;
}
if (dp[tmpx][tmpy-1] > dp[x][y] + (tmpy-1-y))
{
dp[tmpx][tmpy-1] = dp[x][y] + (tmpy-1-y);
q.push(make_pair(tmpx, tmpy-1));
}
}
return dp[dx][dy] == INT_MAX ? -1 : dp[dx][dy];
}
};
运行效率
Your submission beats 90.00% Submissions!