原题链接在这里: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:

  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.

题解:

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;
}
}

跟上The Maze IIThe Maze III.

最新文章

  1. [Erlang 0108] Elixir 入门
  2. .Net程序员飞扬有用的85个工具
  3. final修饰的变量是引用不能改变,还是引用的对象不能改变???
  4. 记录Tomcat7.x热部署配置过程
  5. FineUI小技巧(1)简单的购物车页面
  6. android之广播(二)
  7. Mysql和Oracle数据库concat()函数
  8. POJ 3267 The Cow Lexicon
  9. 3Sum Closest &amp; 3Sum Smaller
  10. 使用WIF实现单点登录Part III —— 正式实战
  11. CentOS中操作Psql
  12. Android Broadcast Receiver
  13. 一个可能是pip的一个BUG
  14. nutch2.2.1
  15. (原+转)ubuntu16中莫名死机及重新安装显卡驱动
  16. Jenkins+SonarQube代码质量检查自动化
  17. Linux的环境变量
  18. 201521123050 《Java程序设计》第13周学习总结
  19. 【CF434D】Nanami&#39;s Power Plant 最小割
  20. linux安装jenkins和编译发布gitlib项目

热门文章

  1. Redis Desktop Manager 0.9.3 版本下载
  2. 软件——解决Modelsim10.1d窗口不停弹出问题(一直弹窗)
  3. c#结束练习题
  4. 接口例_龟车赛跑_Java
  5. https相关知识总结
  6. YUV与RGB互转各种公式 (YUV与RGB的转换公式有很多种,请注意区别!!!)
  7. Docker 镜像,dump openjdk-alpine 镜像容器中的 jvm
  8. AI金融:LSTM预测股票
  9. redis-启用命令
  10. centos7配置jdk8环境变量