随笔 - 164,  文章 - 0,  评论 - 0,  阅读 - 4141

This is graph and look for the shortest path, so BFS should be solution. 

But how to handle K? We can look K as a parameter of every node.

My first solution use two Queues to solve it, one for node in grid, another one for remain number of nodes that can be removed. 

But later I found that using one Queue can do it. The time complexity is O(m*n*k).

复制代码
    public int shortestPath(int[][] grid, int k) {
        int[][] dirs = new int[][]{{0, 1},{-1,0},{0, -1},{1, 0}};
        int m= grid.length;
        int n=grid[0].length;
        Queue<int[]> queue = new LinkedList<>();
        boolean[][][] visited= new boolean[m][n][k+1];
    
        queue.offer(new int[]{0,0,k});
        visited[0][0][k]=true;
        int steps=0;
        
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                int[] node = queue.poll();
                int x = node[0];
                int y=node[1];
                int curK = node[2];
                if(x==m-1 && y==n-1)
                    return steps;
                for(int[] dir:dirs){
                    int newX = x+dir[0];
                    int newY = y+dir[1];
                    if(newX<0|| newX>=m || newY<0||newY>=n)
                        continue;
                    int cellValue = grid[newX][newY];
                    if(cellValue==0){
                        if( visited[newX][newY][curK])
                            continue;
                        queue.offer(new int[]{newX, newY, curK});
                        visited[newX][newY][curK]=true;
                    }else{
                         if(curK==0 || visited[newX][newY][curK-1])
                            continue;
                        queue.offer(new int[]{newX, newY,curK-1});
                        visited[newX][newY][curK-1]=true;
                    }
                }
            }
            steps++;
        }
        return -1;
    }
复制代码

We can make short codes:

复制代码
    public int shortestPath(int[][] grid, int k) {
        int[][] dirs = new int[][]{{0, 1},{-1,0},{0, -1},{1, 0}};
        int m= grid.length;
        int n=grid[0].length;
        Queue<int[]> queue = new LinkedList<>();
        boolean[][][] visited= new boolean[m][n][k+1];
    
        queue.offer(new int[]{0,0,k});  //k is the remain obstacles you can remove
        visited[0][0][k]=true;          //k is the remain obstacles you can remove
        int steps=0;
        
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                int[] node = queue.poll();
                int x = node[0];
                int y=node[1];
                int curK = node[2];
                if(x==m-1 && y==n-1)
                    return steps;
                for(int[] dir:dirs){
                    int newX = x+dir[0];
                    int newY = y+dir[1];
                    int newK = curK;
                    if(newX<0|| newX>=m || newY<0||newY>=n)
                        continue;
                    int cellValue = grid[newX][newY];
                    if(cellValue==1){
                        newK--;
                    }
                    if(newK<0||visited[newX][newY][newK])
                        continue;
                    visited[newX][newY][newK] = true;
                    queue.offer(new int[]{newX, newY, newK});
                }
            }
            steps++;
        }
        return -1;
    }
复制代码

 

posted on   阳光明媚的菲越  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示