Leetcode Maze 总结
来源:互联网 发布:辐射4提取脸部数据 编辑:程序博客网 时间:2024/06/16 01:58
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.
Maze I (BFS)
判断是否可以到达 目的地
public boolean hasPath(int[][] maze, int[] start, int[] destination) { int m = maze.length, n = maze[0].length; boolean[][] visited = new boolean[m][n]; int[] dx = new int[] { 0, 0, 1, -1}; int[] dy = new int[] {-1, 1, 0, 0}; Queue<int[]> queue = new LinkedList<>(); queue.offer(start); visited[start[0]][start[1]] = true; while (!queue.isEmpty()) { int[] curr_pos = queue.poll(); if (curr_pos[0] == destination[0] && curr_pos[1] == destination[1]) return true; for (int i = 0; i < 4; i++) { int x = curr_pos[0], y = curr_pos[1]; while (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == 0) { x += dx[i]; y += dy[i]; } x -= dx[i]; y -= dy[i]; if (!visited[x][y]) { visited[x][y] = true; queue.offer(new int[] {x, y}); } } } return false;
(DFS)
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public boolean hasPath(int[][] maze, int[] start, int[] destination) { if (maze == null || maze.length == 0 || maze[0].length == 0) return false; return dfs(maze, start, destination, new boolean[maze.length][maze[0].length]); } private boolean dfs(int[][] maze, int[] current, int[] des, boolean[][] visited) { if (current[0] == des[0] && current[1] == des[1]) return true; int x = current[0], y = current[1]; int m = maze.length, n = maze[0].length; if (x < 0 || x > m || y < 0 || y > n || visited[x][y]) return false; visited[x][y] = true; for (int i = 0; i < dirs.length; i++) { int dx = x, dy = y; while (dx >= 0 && dx < m && dy >= 0 && dy < n && maze[dx][dy] == 0) { dx += dirs[i][0]; dy += dirs[i][1]; } int[] pos = new int[] {dx -= dirs[i][0], dy -= dirs[i][1]}; if (dfs(maze, pos, des, visited)) return true; } return false; }
Maze II
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.
找到 到达目的地的最短路线
(BFS)
public int shortestDistance(int[][] maze, int[] start, int[] destination) { Queue<int[]> q = new LinkedList<>(); int m = maze.length, n = maze[0].length; int[][] dist = new int[m][n]; for (int i = 0; i < m; i++) { Arrays.fill(dist[i], Integer.MAX_VALUE); } int[] dx = new int[] {-1, 0, 1, 0}; int[] dy = new int[] { 0, 1, 0, -1}; q.offer(start); dist[start[0]][start[1]] = 0; while (!q.isEmpty()) { int[] p = q.poll(); for (int i = 0; i < 4; i++) { int x = p[0] + dx[i], y = p[1] + dy[i]; int cnt = 1; while (x >=0 && x < m && y >= 0 && y < n && maze[x][y] != 1) { x += dx[i]; y += dy[i]; cnt++; } x -= dx[i]; y -= dy[i]; cnt--; if (dist[p[0]][p[1]] + cnt < dist[x][y]) { dist[x][y] = dist[p[0]][p[1]] + cnt; q.offer(new int[] {x, y}); } } } return dist[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : dist[destination[0]][destination[1]]; }
Maze III
public String findShortestWay(int[][] maze, int[] ball, int[] hole) { int m = maze.length; int n = maze[0].length; PriorityQueue<Point> pq = new PriorityQueue<Point>(); Point start = new Point(ball[0], ball[1], 0, ""); pq.offer(start); boolean[][] visited = new boolean[m][n]; String[] directions = {"u", "r", "d", "l"}; int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; while(!pq.isEmpty()) { Point p = pq.poll(); if(p.x == hole[0] && p.y == hole[1]) return p.path; if(visited[p.x][p.y]) continue; visited[p.x][p.y] = true; for(int i = 0; i < dirs.length; i++) { int x = p.x; int y = p.y; int len = p.len; while(x + dirs[i][0] >= 0 && x + dirs[i][0] < m && y + dirs[i][1] >= 0 && y + dirs[i][1] < n && maze[x + dirs[i][0]][y + dirs[i][1]] != 1 && (x != hole[0] || y != hole[1])) { x += dirs[i][0]; y += dirs[i][1]; len++; } Point next = new Point(x, y, len, p.path + directions[i]); pq.offer(next); } } return "impossible"; } class Point implements Comparable<Point> { int x; int y; int len; String path; Point(int x, int y) { this.x = x; this.y = y; len = Integer.MAX_VALUE; path = ""; } Point(int x, int y, int len, String path) { this.x = x; this.y = y; this.len = len; this.path = path; } public int compareTo(Point p) { return this.len == p.len? this.path.compareTo(p.path) : this.len - p.len; } }
- Leetcode Maze 总结
- Maze
- maze
- Maze
- maze
- Maze
- maze
- Maze
- Maze
- Maze
- Leetcode 512, Mouse in a maze
- [Leetcode] 490. The Maze 解题报告
- [Leetcode] 499. The Maze III 解题报告
- [Leetcode] 505. The Maze II 解题报告
- LeetCode总结
- leetcode 总结
- LeetCode总结
- LeetCode总结
- Linux_博客-URG-PSH
- Bugzilla使用
- Shape的使用---带色的圆点
- Excel VBA选择文件、高容错性地打开文件
- TensorFlow Serving,GPU版安装
- Leetcode Maze 总结
- PAT乙级1023. 组个最小数 (20)
- EXCEL VBA 正则表达式
- 移动端WEB开发,click,touch,tap事件浅析
- oracle order by 处理NULL 值
- python练习2
- Session机制详解
- 【剑指offer】题51:数组中重复的数字
- DataTables上移下移排序、拖拽排序