为高尔夫比赛砍树2022-05-23每日一题 hard

题目描述

你被请来给一个要举办高尔夫比赛的树林砍树。树林由一个 m x n 的矩阵表示, 在这个矩阵中:
0 表示障碍,无法触碰
1 表示地面,可以行走
比 1 大的数 表示有树的单元格,可以行走,数值表示树的高度
每一步,你都可以向上、下、左、右四个方向之一移动一个单位,如果你站的地方有一棵树,那么你可以决定是否要砍倒它
你需要按照树的高度从低向高砍掉所有的树,每砍过一颗树,该单元格的值变为 1(即变为地面)。
你将从 (0, 0) 点开始工作,返回你砍完所有树需要走的最小步数。 如果你无法砍完所有的树,返回 -1 。
可以保证的是,没有两棵树的高度是相同的,并且你至少需要砍倒一棵树。

在这里插入图片描述

思路分析:

大致意思就是砍树,但是有规则。必须从小往大砍,其中0表示障碍,1是地面,大于1的是树(树砍完以后就成了地面。)

  • 首先将矩阵中的树从小到大排序(数组中存的是每一棵树的位置)
  • 通过bfs找到从某一个位置到另一个位置的最短路径(求最少移动步数,一般可以使用 BFS 去做)
  • 最后求和所有的最短路径

挨个分析

  • 首先将矩阵中的树从小到大排序(数组中存的是每一棵树的位置)

只要将矩阵中大于1的树按照树的高度排序即可

// forest 表示所有的树   list 表示返回的排序的树的位置
private void getAllWithSort(List<List<Integer>> forest, List<int[]> list) {
        int N = forest.size();
        int M = forest.get(0).size();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (forest.get(i).get(j) > 1) {
                    list.add(new int[] {i, j});
                }
            }
        }
        Collections.sort(list, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return forest.get(o1[0]).get(o1[1]) - forest.get(o2[0]).get(o2[1]);
            }
        });
    }
  • 通过bfs找到从某一个位置到另一个位置的最短路径(求最少移动步数,一般可以使用 BFS 去做)

bfs 一般使用队列来做

模板一
  • 若不需要知道当前层数,只需要访问完所有节点就可以时
while queue 不空:
    cur = queue.pop()
    if cur 有效且未被访问过:
        进行处理
    for 节点 in cur 的所有相邻节点:
        if 该节点有效:
            queue.push(该节点)
模板二
  • 如果要确定当前遍历到了哪一层,需要知道最少移动步数时

这里增加了 step 表示在一个图中,现在已经走了多少步了。size 表示在当前遍历层有多少个元素,也就是队列中的元素数,我们把这些元素一次性遍历完,即把当前层的所有元素都向外走了一步。

step = 0
while queue 不空:
    size = queue.size()
    while (size --) {
        cur = queue.pop()
        if cur 有效且未被访问过:
            进行处理
        for 节点 in cur的所有相邻节点:
            if 该节点有效:
                queue.push(该节点)
    }
    step++;

本题bfs

int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};;
private int process(List<List<Integer>> forest, int startX, int startY, Integer targetX, Integer targetY) {
    int N = forest.size();
    int M = forest.get(0).size();
    boolean[][] visited = new boolean[N][M];
    Queue<int[]> queue = new LinkedList<>();
    queue.add(new int[] {startX, startY});
    visited[startX][startY] = true;
    int step = 0;

    while (!queue.isEmpty()) {
        int size = queue.size();
        while (size-- > 0) {
            int[] poll = queue.poll();
            if (poll[0] == targetX && poll[1] == targetY) {
                return step;
            }
            for (int i = 0; i < 4; i++) {
                int curX = poll[0] + directions[i][0];
                int curY = poll[1] + directions[i][1];

                if (curX >=0 && curY >= 0 && curX < N && curY < M && forest.get(curX).get(curY) != 0 && !visited[curX][curY]) {
                    queue.add(new int[] {curX, curY});
                    visited[curX][curY] = true;
                }

            }
        }
        step++;
    }
    return -1;
}

总的代码

class Solution {
    public int cutOffTree(List<List<Integer>> forest) {

        List<int[]> list = new ArrayList<>();
        getAllWithSort(forest, list);
        int res = 0;
        int x = 0;
        int y = 0;
        for (int i = 0; i < list.size(); i++) {

            int step = process(forest, x, y, list.get(i)[0], list.get(i)[1]);
            if (step == -1) {
                return -1;
            }
            res += step;
            x = list.get(i)[0];
            y = list.get(i)[1];
        }

        return res;

    }

    // bfs
    int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};;
    private int process(List<List<Integer>> forest, int startX, int startY, Integer targetX, Integer targetY) {

        int N = forest.size();
        int M = forest.get(0).size();

        boolean[][] visited = new boolean[N][M];
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[] {startX, startY});
        visited[startX][startY] = true;
        int step = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                int[] poll = queue.poll();
                if (poll[0] == targetX && poll[1] == targetY) {
                    return step;
                }
                for (int i = 0; i < 4; i++) {
                    int curX = poll[0] + directions[i][0];
                    int curY = poll[1] + directions[i][1];

                    if (curX >=0 && curY >= 0 && curX < N && curY < M && forest.get(curX).get(curY) != 0 && !visited[curX][curY]) {
                        queue.add(new int[] {curX, curY});
                        visited[curX][curY] = true;
                    }

                }
            }
            step++;
        }
        return -1;
    }

    // 1 是地面,不用砍树,直接行走。
    private void getAllWithSort(List<List<Integer>> forest, List<int[]> list) {

        int N = forest.size();
        int M = forest.get(0).size();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (forest.get(i).get(j) > 1) {
                    list.add(new int[] {i, j});
                }
            }
        }
        Collections.sort(list, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return forest.get(o1[0]).get(o1[1]) - forest.get(o2[0]).get(o2[1]);
            }
        });
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值