675. 为高尔夫比赛砍树 bfs

import java.util.*;

public class Solution {


    public int cutOffTree(List<List<Integer>> forest) {
        int n = forest.size();
        int m = forest.get(0).size();
        List<int[]> trees = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (forest.get(i).get(j) > 1) {
                    trees.add(new int[]{i, j, forest.get(i).get(j)});
                }
            }
        }

        Collections.sort(trees, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[2] - o2[2];
            }
        });
        int start_x = 0, start_y = 0;
        int[] ch_x = {1, -1, 0, 0};
        int[] ch_y = {0, 0, 1, -1};
        boolean can_do = true;
        int ans = 0;

        for (int[] tree : trees) {
            int dest_x = tree[0];
            int dest_y = tree[1];
            int[][] bj = new int[n][m];
            Queue<int[]> queue = new LinkedList<>();
            queue.add(new int[]{start_x, start_y, 0});
            boolean find_it = false;


            while (!queue.isEmpty()) {
                int[] now = queue.remove();
                int now_x = now[0];
                int now_y = now[1];
                int now_step = now[2];
                bj[now_x][now_y] = 1;
                if (now_x == dest_x && now_y == dest_y) {
                    find_it = true;
                    ans += now_step;
                    break;
                }
                for (int i = 0; i < 4; i++) {
                    int next_x = now_x + ch_x[i];
                    int next_y = now_y + ch_y[i];
                    if (next_x >= 0 && next_x < n && next_y < m && next_y >= 0) {
                        if (bj[next_x][next_y] == 0) {
                            if (forest.get(next_x).get(next_y) > 0) {
                                bj[next_x][next_y] = 1;
                                //注意上面这一句,读取下一步数据之前就要标记,同一个地方会进入多次,bfs会很慢
                                queue.add(new int[]{next_x, next_y, now_step + 1});
                            }
                        }
                    }
                }
            }
            if (!find_it) {
                can_do = false;
                break;
            } else {
                forest.get(dest_x).set(dest_y, 1);
                start_x = tree[0];
                start_y = tree[1];
            }
        }
        if (!can_do)
            return -1;
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        Integer[][] temp = new Integer[][]
                {{69438, 55243, 0, 43779, 5241, 93591, 73380}, {847, 49990, 53242, 21837, 89404, 63929, 48214}, {90332, 49751, 0, 3088, 16374, 70121, 25385}, {14694, 4338, 87873, 86281, 5204, 84169, 5024}, {31711, 47313, 1885, 28332, 11646, 42583, 31460}, {59845, 94855, 29286, 53221, 9803, 41305, 60749}, {95077, 50343, 27947, 92852, 0, 0, 19731}, {86158, 63553, 56822, 90251, 0, 23826, 17478}, {60387, 23279, 78048, 78835, 5310, 99720, 0}, {74799, 48845, 60658, 29773, 96129, 90443, 14391}, {65448, 63358, 78089, 93914, 7931, 68804, 72633}, {93431, 90868, 55280, 30860, 59354, 62083, 47669}, {81064, 93220, 22386, 22341, 95485, 20696, 13436}, {50083, 0, 89399, 43882, 0, 13593, 27847}, {0, 12256, 33652, 69301, 73395, 93440, 0}, {42818, 87197, 81249, 33936, 7027, 5744, 64710}, {35843, 0, 99746, 52442, 17494, 49407, 63016}, {86042, 44524, 0, 0, 26787, 97651, 28572}, {54183, 83466, 96754, 89861, 84143, 13413, 72921}, {89405, 52305, 39907, 27366, 14603, 0, 14104}, {70909, 61104, 70236, 30365, 0, 30944, 98378}, {20124, 87188, 6515, 98319, 78146, 99325, 88919}, {89669, 0, 64218, 85795, 2449, 48939, 12869}, {93539, 28909, 90973, 77642, 0, 72170, 98359}, {88628, 16422, 80512, 0, 38651, 50854, 55768}, {13639, 2889, 74835, 80416, 26051, 78859, 25721}, {90182, 23154, 16586, 0, 27459, 3272, 84893}, {2480, 33654, 87321, 93272, 93079, 0, 38394}, {34676, 72427, 95024, 12240, 72012, 0, 57763}, {97957, 56, 83817, 45472, 0, 24087, 90245}, {32056, 0, 92049, 21380, 4980, 38458, 3490}, {21509, 76628, 0, 90430, 10113, 76264, 45840}, {97192, 58807, 74165, 65921, 45726, 47265, 56084}, {16276, 27751, 37985, 47944, 54895, 80706, 2372}, {28438, 53073, 0, 67255, 38416, 63354, 69262}, {23926, 75497, 91347, 58436, 73946, 39565, 10841}, {34372, 69647, 44093, 62680, 32424, 69858, 68719}, {24425, 4014, 94871, 1031, 99852, 88692, 31503}, {24475, 12295, 33326, 37771, 37883, 74568, 25163}, {0, 18411, 88185, 60924, 29028, 69789, 0}, {34697, 75631, 7636, 16190, 60178, 39082, 7052}, {24876, 9570, 53630, 98605, 22331, 79320, 88317}, {27204, 89103, 15221, 91346, 35428, 94251, 62745}, {26636, 28759, 12998, 58412, 38113, 14678, 0}, {80871, 79706, 45325, 3861, 12504, 0, 4872}, {79662, 15626, 995, 80546, 64775, 0, 68820}, {25160, 82123, 81706, 21494, 92958, 33594, 5243}};

        List<List<Integer>> forest = new ArrayList<>();
        for (int i = 0; i < temp.length; i++) {
            forest.add((Arrays.asList(temp[i])));
        }
        int ans = solution.cutOffTree(forest);
        System.out.println(ans);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值