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 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: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
Example 2
Note:
There is only one ball and one destination in the maze.
Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
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.
The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
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: false
Explanation: There is no way for the ball to stop at the destination.
class Solution {
private int[] dr;
private int[] dc;
private int[][] MAZE;
private int R;
private int C;
private int[] dest;
private boolean res;
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
dr = new int[]{-1, 1, 0, 0}; // u d l r
dc = new int[]{0, 0, -1, 1}; // u d l r
MAZE = maze;
R = maze.length;
C = maze[0].length;
dest = destination;
res = false;
dfs(start, new boolean[R][C]);
return res;
}
private void dfs(int[] start, boolean[][] visited) {
int r = start[0], c = start[1];
if (Arrays.equals(start, dest)) {
res = true;
return;
}
if (visited[r][c]) return;
visited[r][c] = true;
// up down left right
for (int i = 0; i < 4; ++i) {
int x = r, y = c;
while (isValid(new int[]{x + dr[i], y + dc[i]})) {
x += dr[i];
y += dc[i];
}
dfs(new int[]{x, y}, visited);
}
}
// return true if the coord is valid and maze[coord] == 0;
private boolean isValid(int[] coord) {
int r = coord[0];
int c = coord[1];
if (r < 0 || r >= R || c < 0 || c >= C) return false;
if (MAZE[r][c] == 1) return false;
return true;
}
}
# BFS Solution;
class Solution(object):
def hasPath(self, maze, start, destination):
"""
:type maze: List[List[int]]
:type start: List[int]
:type destination: List[int]
:rtype: bool
"""
# return true if input is valid and maze[input]== 0
def isValid(coord):
r = coord[0]
c = coord[1]
if r < 0 or r >= R or c < 0 or c >= C:
return False
if maze[r][c] == 1:
return False
return True
# 先定义变量
R = len(maze)
C = len(maze[0])
dr = [-1, 1, 0, 0] # u,d,l,r
dc = [0, 0, -1, 1]
queue = collections.deque()
visited = [[False for i in range(C)] for j in range(R)]
queue.appendleft(tuple(start))
# BFS
while queue:
curr = queue.pop()
r, c = curr
if visited[r][c]: continue
else: visited[r][c] = True
for i in range(4):
newCoord = [r, c]
while isValid((newCoord[0] + dr[i], newCoord[1] + dc[i])):
newCoord[0] += dr[i]
newCoord[1] += dc[i]
if newCoord == destination:
return True
queue.appendleft(tuple(newCoord))
return False
class Solution {
const int dr[4]{0, -1, 0, 1};
const int dc[4]{-1, 0, 1, 0};
bool dfs(const vector<vector<int>>& maze, vector<int> start, vector<int>& destination,
unordered_set<string>& visited) {
if (visited.count(to_string(start[0]) + "," + to_string(start[1]))) return false;
else if (start == destination) return true;
visited.insert(to_string(start[0]) + "," + to_string(start[1]));
for (int i = 0; i < 4; ++i) {
int r = start[0], c = start[1];
while (isValid(maze, r + dr[i], c + dc[i])) {
r += dr[i];
c += dc[i];
}
if (dfs(maze, vector<int>{r, c}, destination, visited)) return true;
}
return false;
}
bool isValid(const vector<vector<int>>& maze, int r, int c) {
if (r < 0 || r >= maze.size() || c < 0 || c >= maze[0].size()) return false;
else if (maze[r][c] == 1) return false;
return true;
}
public:
bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
unordered_set<string> visited;
return dfs(maze, start, destination, visited);
}
};