LeetCode - Cut Off Trees for Golf Event

本文探讨了一种在二维地图上按树高顺序砍伐树木的算法挑战,使用优先队列和BFS策略来找到最短路径,以最小化行走步数。文章详细描述了算法实现过程,包括初始化森林地图、确定树木位置、执行砍伐操作和计算总步数的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:

0 represents the obstacle can't be reached.
1 represents the ground can be walked through.
The place with number bigger than 1 represents a tree can be walked through, and this positive number represents the tree's height.
You are asked to cut off all the trees in this forest in the order of tree's height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).

You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can't cut off all the trees, output -1 in that situation.

You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off.

Example 1:
Input: 
[
 [1,2,3],
 [0,0,4],
 [7,6,5]
]
Output: 6
Example 2:
Input: 
[
 [1,2,3],
 [0,0,0],
 [7,6,5]
]
Output: -1
Example 3:
Input: 
[
 [2,3,4],
 [0,0,5],
 [8,7,6]
]
Output: 6
Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.
Hint: size of the given matrix will not exceed 50x50.

我用了priorityqueue+bfs 来做,但没有得到正解,目前不知道错在哪儿。。。?

class Solution {
    public int cutOffTree(List<List<Integer>> forest) {
        if(forest == null || forest.size() == 0 || forest.get(0).size() == 0 || forest.get(0).get(0) == 0){
            return -1;
        }
        int row = forest.size();
        int column = forest.get(0).size();        
        PriorityQueue<int[]> pqueue = new PriorityQueue<>((a,b) -> compare(a, b));
        int count = 0;
        for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                int height = forest.get(i).get(j);
                if(height > 1 && count < 2){
                    count++;
                    Queue<int[]> queue = new LinkedList<>();
                    pqueue.add(new int[]{i,j,height});
                    queue.add(new int[]{i,j});
                    forest.get(i).set(j,1);
                    while(!queue.isEmpty()){
                        int[] temp = queue.poll();
                        int r = temp[0];
                        int c = temp[1];
                        //move up
                        if(r-1 >= 0 && forest.get(r-1).get(c) > 1){
                            queue.add(new int[]{r-1,c});
                            pqueue.add(new int[]{r-1,c,forest.get(r-1).get(c)});
                            forest.get(r-1).set(c,1);
                        }
                        //move down
                        if(r+1 < row && forest.get(r+1).get(c) > 1){
                            queue.add(new int[]{r+1,c});
                            pqueue.add(new int[]{r+1,c,forest.get(r+1).get(c)});
                            forest.get(r+1).set(c,1);
                        }                        
                        //move left
                        if(c-1 >= 0 && forest.get(r).get(c-1) > 1){
                            queue.add(new int[]{r,c-1});
                            pqueue.add(new int[]{r,c-1,forest.get(r).get(c-1) });
                            forest.get(r).set(c-1,1);                           
                        }
                        //move right
                        if(c+1 < column && forest.get(r).get(c+1) > 1){
                            queue.add(new int[]{r,c+1});
                            pqueue.add(new int[]{r,c+1,forest.get(r).get(c+1)});
                            forest.get(r).set(c+1,1);                               
                        }
                    }
                }
                else if(count >= 2){
                    return -1;
                }
            }
        }   
        int steps = 0;
        int[] start = new int[]{0,0};
        for(int[] end : pqueue){
            steps = steps + getSteps(start, end, forest, row, column);
            start = end;   
        }
        
        return steps;
    }
    
    private int getSteps(int[] start, int[] end, List<List<Integer>> forest, int row, int column){
        Queue<int[]> path = new LinkedList<>();
        int count = 0;
        boolean found = false;
        path.offer(start);
        while(!found){
            Queue<int[]> temp = new LinkedList<>();
            while(!path.isEmpty()){
                int[] point = path.poll();
                int r = point[0];
                int c = point[1];
                if(r == end[0] && c == end[1]){
                    found = true;
                    break;
                }
                //move up
                if(r - 1 >= 0 && forest.get(r-1).get(c) >= 1){
                    temp.offer(new int[]{r-1,c});
                }
                //move down
                if(r + 1 < row && forest.get(r+1).get(c) >= 1){
                    temp.offer(new int[]{r+1,c});
                }            
                //move left
                if(c - 1 >= 0 && forest.get(r).get(c-1) >= 1){
                    temp.offer(new int[]{r,c-1});
                }                
                //move right
                if(c + 1 < column && forest.get(r).get(c+1) >= 1){
                    temp.offer(new int[]{r,c+1});
                }
            }
            if(!found){
                count++;
                path = temp;
            }
        }
        return count;
    }
    private int compare(int[] a, int[] b){
        int heightA = a[2];
        int heightB = b[2];
        return heightA - heightB;
    }
}

 

转载于:https://www.cnblogs.com/incrediblechangshuo/p/9734023.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值