LeetCode 490. The Maze
原题链接在这里:https://leetcode.com/problems/the-maze/
题目:
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:
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.
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.
题解:
Starting from start index, perform BFS on 4 directions. For each direction, only stops when hitting the boundary or wall.
Then check if hitting position is visited before. If not, mark it as visited, add it to queue.
Time Complexity: O(m*n). m = maze.length. n = maze[0].length.
Space: O(m*n).
AC Java:
class Solution {
int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0,-1}, {0,1}}; public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if(maze == null || maze.length == 0 || maze[0].length == 0){
return false;
} int m = maze.length;
int n = maze[0].length;
if(start[0]<0 || start[0]>=m || start[1]<0 || start[1]>=n ||
destination[0]<0 || destination[0]>=m || destination[1]<0 || destination[1]>=n){
return false;
} LinkedList<int []> que = new LinkedList<>();
boolean [][] visited = new boolean[m][n];
que.add(start);
visited[start[0]][start[1]] = true;
while(!que.isEmpty()){
int [] cur = que.poll();
if(cur[0] == destination[0] && cur[1] == destination[1]){
return true;
} for(int [] dir : dirs){
int r = cur[0];
int c = cur[1];
while(r+dir[0]>=0 && r+dir[0]<m && c+dir[1]>=0 && c+dir[1]<n && maze[r+dir[0]][c+dir[1]]==0){
r += dir[0];
c += dir[1];
} if(!visited[r][c]){
visited[r][c] = true;
que.add(new int[]{r, c});
}
}
} return false;
}
}
Could also use DFS. DFS state needs maze, current starting point, destinaiton point, visited matrix and current moving direction.
If starting point == destinaiton point, return true.
Otherwise, for each of 4 directions, calculae the stop point.
If the stop point hasn't been visited before, mark it as visited and continue DFS from that point. If any of 4 dirs, dfs result from that point return true, then return true.
Time Complexity: O(m*n).
Space: O(m*n).
AC Java:
class Solution {
int [][] dirs = new int[][]{{-1, 0}, {1,0}, {0,-1}, {0,1}};
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if(maze == null || maze.length == 0 || maze[0].length == 0){
return false;
} int m = maze.length;
int n = maze[0].length;
boolean [][] visited = new boolean[m][n];
visited[start[0]][start[1]] = true; for(int [] dir : dirs){
if(dfs(maze, start, destination, dir, visited)){
return true;
}
} return false;
} private boolean dfs(int[][] maze, int[] start, int[] dest, int [] dir, boolean [][] visited){
int m = maze.length;
int n = maze[0].length; int r = start[0];
int c = start[1]; if(r == dest[0] && c ==dest[1]){
return true;
} while(r+dir[0]>=0 && r+dir[0]<m && c+dir[1]>=0 && c+dir[1]<n && maze[r+dir[0]][c+dir[1]]==0){
r += dir[0];
c += dir[1];
} if(visited[r][c]){
return false;
} visited[r][c] = true;
for(int [] nextDir : dirs){
if(dfs(maze, new int[]{r,c}, dest, nextDir, visited)){
return true;
}
} return false;
}
}
最新文章
- [Erlang 0108] Elixir 入门
- .Net程序员飞扬有用的85个工具
- final修饰的变量是引用不能改变,还是引用的对象不能改变???
- 记录Tomcat7.x热部署配置过程
- FineUI小技巧(1)简单的购物车页面
- android之广播(二)
- Mysql和Oracle数据库concat()函数
- POJ 3267 The Cow Lexicon
- 3Sum Closest &; 3Sum Smaller
- 使用WIF实现单点登录Part III —— 正式实战
- CentOS中操作Psql
- Android Broadcast Receiver
- 一个可能是pip的一个BUG
- nutch2.2.1
- (原+转)ubuntu16中莫名死机及重新安装显卡驱动
- Jenkins+SonarQube代码质量检查自动化
- Linux的环境变量
- 201521123050 《Java程序设计》第13周学习总结
- 【CF434D】Nanami&#39;s Power Plant 最小割
- linux安装jenkins和编译发布gitlib项目