[Daily question] DFS with restrictions——escape from the maze, resume update today

1036. Escape from the maze\color{red}{1036. Escape from the maze}1 0 3 6 .Escape from the big maze

In a 10^6 x 10^6 grid, the coordinates of each grid cell are (x, y).

Now source = [sx, sy]starting from the source square, the intention is to rush to the target square target = [tx, ty]. The array blockedis a list of blocked squares, each of which blocked[i] = [xi, yi]indicates that (xi, yi)the square with coordinates is forbidden to pass.

Every time you move, you can go to the adjacent squares in the grid in four directions, as long as the square is not on the given block list blocked. At the same time, no stepping off the grid is allowed.

Only return if it is possible to get from the source square sourceto the destination square by a sequence of moves . Otherwise, return .targettruefalse

Data (key) constraints:

  • 0 <= blocked.length <= 200

Problem-solving ideas:

  1. First of all, for the problem of finding a path in this kind of grid, it is generally solved by a search algorithm-selecting DFS or BFS, and other better search algorithms according to whether the shortest path is required, the size of the data, etc.
  2. Due to the amount of data in this question, and there is no requirement to find the shortest "escape" path, if you expand gradually through BFS, you will definitely have no chance; consider DFS. According to the Hard difficulty of this question, it is not difficult to think of using optimized DFS algorithm - the most common optimization is pruning optimization (there are three commonly used pruning strategies: memorization - the same value extracts the result from the memory array, optimal solution judgment - if the current branch is worse than the current optimal pruning, Feasibility pruning - the current branch cannot be unpruned)
  3. But this question should not be pruning, but a distance-limited DFS search: due to a special data constraint, the blocked grid is only 200 grids at most, assuming that these grids are sorted by slashes to achieve the best blocking effect, similar to the following, as long as The Manhattan distance between the point reached by DFS and the origin reaches a certain range, that is, the blockade must be broken. This range is not difficult to obtain as blocked.length according to the best blocking effect (Figure 1)
  4. Therefore, when DFS reaches a point a certain distance from the origin, or directly searches for the target, it means that the node is "free"

Problem-solving code:

There is still a lot of room for improvement:

class Solution {
    
    
    //封锁格子数量
    int len;
    //上下左右方向数组
    int[][] d;
    //保存访问信息
    Map<Integer,Set<Integer>> v;
    //全局block
    int[][] b;

    public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
    
    
        b=blocked;
        len=blocked.length;
        d=new int[][]{
    
    {
    
    -1,0},{
    
    1,0},{
    
    0,-1},{
    
    0,1}};

        //访问原节点
        v=new HashMap<>();
        Set<Integer> sou=new HashSet<Integer>();
        sou.add(source[1]);
        v.put(source[0],sou);

        //判断原节点是否free
        if(dfs(source,source,target)){
    
    
            
            
            v=new HashMap<>();
            Set<Integer> tar=new HashSet<Integer>();
            tar.add(target[1]);
            v.put(target[0],tar);

            //判断目标节点是否free
            if(dfs(target,target,source))
                return true;
        }

        return false;
    }

    public boolean dfs(int[] cur,int[] o,int[] t){
    
    
        //如果超过范围,或者到达目标返回true
        if((Math.abs(cur[0]-o[0])+Math.abs(cur[1]-o[1]))>=len||(cur[0]==t[0]&&cur[1]==t[1])){
    
    
            return true;
        }
        else{
    
    
            for(int i=0;i<4;i++){
    
    
                int mx=cur[0]+d[i][0];
                int my=cur[1]+d[i][1];
                if(mx<0||mx>=1000000||my<0||my>=1000000||judge(mx,my)){
    
    
                    continue;
                }
                else{
    
    
                    Set<Integer> j=v.get(mx);
                    if(j==null||!j.contains(my)){
    
    
                        if(j==null){
    
    
                            j=new HashSet<Integer>();
                        }
                        j.add(my);
                        v.put(mx,j);
                        if(dfs(new int[]{
    
    mx,my},o,t))
                            return true;
                    }
                    else{
    
    
                        continue;
                    }
                }
            }
        }
        return false;
    }

    //判断是否是blocked
    public boolean judge(int x,int y){
    
    
        for(int i=0;i<len;i++){
    
    
            if(b[i][0]==x&&b[i][1]==y)
                return true;
        }
        return false;
    }

}

end

Question source: LeetCode link: https://leetcode-cn.com/problems

⭐️Follow the author, take you to brush the questions, and learn the most commonly used algorithm skills from simple algorithm questions (one question per day during the winter vacation) ⭐️Follow the
author to brush the questions—from simple to advanced, let you become a ruthless machine for brushing questions without knowing it , if you have any questions, please private message

Guess you like

Origin blog.csdn.net/caqjeryy/article/details/123507430