1. 程式人生 > >675. 為高爾夫比賽砍樹 bfs

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);
    }

}