资讯专栏INFORMATION COLUMN

490. The Maze && 505. The Maze II

BoYang / 1899人阅读

摘要:题目链接和上一题不一样的是这道题要求最短的路径,普通的遍历和都是可以做的,但是求最短路径的话还是用。这里相当于每个点有至多条相连,每条的就是到墙之前的长度。

490. The Maze

题目链接:https://leetcode.com/problems...

又是图的遍历问题,就是简单的遍历,所以dfs和bfs都可以做,复杂度也是一样的。这道题要求球不能停下来,即使碰到destination,必须是碰到wall才能停下来。

public class Solution {
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        if(maze.length == 0 || maze[0].length == 0) return false;
        if(start[0] == destination[0] && start[1] == destination[1]) return true;
        
        m = maze.length; n = maze[0].length;
        boolean[][] visited = new boolean[m][n];
        return dfs(maze, start, destination, visited);
    }
    int m, n;
    int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    private boolean dfs(int[][] maze, int[] cur, int[] dest, boolean[][] visited) {
        // already visited
        if(visited[cur[0]][cur[1]]) return false;
        // reach destination
        if(Arrays.equals(cur, dest)) return true;
        
        visited[cur[0]][cur[1]] = true;
        for(int[] dir : dirs) {
            int nx = cur[0], ny = cur[1];
            while(notWall(nx + dir[0], ny + dir[1]) && maze[nx+dir[0]][ny+dir[1]] != 1) {
                nx += dir[0]; ny += dir[1];
            }
            if(dfs(maze, new int[] {nx, ny}, dest, visited)) return true;
        }
        return false;
    }
    
    private boolean notWall(int x, int y) {
        return x >= 0 && x < m && y >= 0 && y < n;
    }
}
505. The Maze II

题目链接:https://leetcode.com/problems...

和上一题不一样的是:这道题要求最短的路径,普通的遍历dfs和bfs都是可以做的,但是求最短路径的话还是用Dijksra。这里相当于每个点有至多4条edge相连,每条edge的weight就是到墙之前的长度。

public class Solution {
    public int shortestDistance(int[][] maze, int[] start, int[] destination) {
        // base case
        if(Arrays.equals(start, destination)) return 0;
        
        m = maze.length; n = maze[0].length;
        
        return shortestPath(maze, start, destination);
    }
    int m, n;
    int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    private int shortestPath(int[][] maze, int[] start, int[] destination) {
        // get the vertice has the minimum distance to start
        PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a.distance - b.distance);
        minHeap.offer(new Node(start[0], start[1], 0));
        
        // map that contains information of node: distance to start point
        int[][] visited = new int[m][n];
        for(int[] arr : visited) Arrays.fill(arr, Integer.MAX_VALUE);
        
        while(!minHeap.isEmpty()) {
            Node cur = minHeap.poll();
            // find the shortest path
            if(cur.x == destination[0] && cur.y == destination[1]) return cur.distance;
            
            for(int[] dir : dirs) {
                int nx = cur.x, ny = cur.y;
                while(isInMaze(nx + dir[0], ny + dir[1]) && maze[nx + dir[0]][ny + dir[1]] != 1) {
                    nx += dir[0];  ny += dir[1];
                }
                int distance = cur.distance + Math.abs(nx - cur.x) + Math.abs(ny - cur.y);
                if(visited[nx][ny] > distance) {
                    minHeap.offer(new Node(nx, ny, distance));
                    visited[nx][ny] = distance;
                }
            }
        }
        return -1;
    }
    
    private boolean isInMaze(int x, int y) {
        return x >= 0 && x < m && y >= 0 && y < n;
    }
    
    class Node {
        int x;
        int y;
        // distance to start point
        int distance;
        Node(int x, int y, int distance) {
            this.x = x;
            this.y = y;
            this.distance = distance;
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66651.html

相关文章

  • [LeetCode] 490. The Maze (BFS/DFS)

    Problem 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 wont stop rolling until hitting a wall. When the ball sto...

    smartlion 评论0 收藏0
  • The Maze II

    摘要:题目链接一般这种题,给一个起点,给一个终点,最后要求最短的路径,都是用来解。的图不是很大,也能。 The Maze II 题目链接:https://leetcode.com/contest/...一般这种题,给一个起点,给一个终点,最后要求最短的路径,都是用bfs来解。 public class Solution { public String findShortestWay(...

    luffyZh 评论0 收藏0
  • canvas实例 --- 制作简易迷宫

    摘要:整个思路十分简单首先我们将迷宫视为一个行列的单元格组合,每一个单元格便可以表示为。用来储存我们已访问过的单元格,则记录我们的访问路径。我们通过将单元格的,,,属性设置为或来标识这个方向是否应该有边框,同时该方向是否可走。 这个系列分为两部分,第一部分为迷宫的生成及操作,第二部分为自动寻路算法。 我们先看效果:点击查看 我们直入正题,先说一说生成迷宫的思路。 整个思路十分简单: 首先...

    孙吉亮 评论0 收藏0
  • canvas实例 --- 制作简易迷宫

    摘要:整个思路十分简单首先我们将迷宫视为一个行列的单元格组合,每一个单元格便可以表示为。用来储存我们已访问过的单元格,则记录我们的访问路径。我们通过将单元格的,,,属性设置为或来标识这个方向是否应该有边框,同时该方向是否可走。 这个系列分为两部分,第一部分为迷宫的生成及操作,第二部分为自动寻路算法。 我们先看效果:点击查看 我们直入正题,先说一说生成迷宫的思路。 整个思路十分简单: 首先...

    twohappy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<