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, determine whether the ball could stop at the destination.
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:
map =
[
[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]
]
start = [0,4]
end = [3,2]
Output:
false
Example 2:
Input:
map =
[[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]
]
start = [0,4]
end = [4,4]
Output:
true
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.
5.The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
分析
这道题跟传统的BFS解法有一点不同。传统的BFS解法只需要向向下左右寻找满足要求的点即可。但是这一题里,只有小球撞到墙时才能转向。也就是说我们在向队列中添加坐标时一定是靠墙的坐标。我们可以使用set保留所有已经访问过的坐标,在队列每弹出一个新的坐标时,需要向上下左右一定寻找到靠墙的坐标,然后判断是否在set中,如果没有,则加入队列中。
Code
class Solution {
public:
/**
* @param maze: the maze
* @param start: the start
* @param destination: the destination
* @return: whether the ball could stop at the destination
*/
bool hasPath(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;
int sx = start[0] + 1;
int sy = start[1] + 1;
int dx = destination[0] + 1;
int dy = destination[1] + 1;
set<pair<int, int>> s;
queue<pair<int, int>> q;
q.push(make_pair(sx, sy));
s.insert(make_pair(sx, sy));
return findRoute(maze, q, s, dx, dy);
}
bool findRoute(vector<vector<int>>& maze, queue<pair<int, int>> q, set<pair<int, int>> &s, int dx, int dy)
{
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
if (x == dx && y == dy)
return true;
int tmpx = x;
int tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpx --;
}
if (s.find(make_pair(tmpx+1, tmpy)) == s.end())
{
q.push(make_pair(tmpx+1, tmpy));
s.insert(make_pair(tmpx+1, tmpy));
}
tmpx = x;
tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpx ++;
}
if (s.find(make_pair(tmpx-1, tmpy)) == s.end())
{
q.push(make_pair(tmpx-1, tmpy));
s.insert(make_pair(tmpx-1, tmpy));
}
tmpx = x;
tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpy --;
}
if (s.find(make_pair(tmpx, tmpy+1)) == s.end())
{
q.push(make_pair(tmpx, tmpy+1));
s.insert(make_pair(tmpx, tmpy+1));
}
tmpx = x;
tmpy = y;
while (maze[tmpx][tmpy] != 1)
{
tmpy ++;
}
if (s.find(make_pair(tmpx, tmpy-1)) == s.end())
{
q.push(make_pair(tmpx, tmpy-1));
s.insert(make_pair(tmpx, tmpy-1));
}
}
return false;
}
};
运行效率
Your submission beats 100.00% Submissions!