LWC 49:675. Cut Off Trees for Golf Event

LWC 49:675. Cut Off Trees for Golf Event

传送门:675. Cut Off Trees for Golf Event

Problem:

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.

Note:

size of the given matrix will not exceed 50x50.

英语是硬伤,这都能理解错误,min其实是唬人的,题目中要求从最低的树开始砍起,所以每次找到最低树的位置,以最小代价走到那即可。

首先维护树高和对应位置的map,方便后续查找。接着从当前点求出能抵达位置的最小代价,最后一步一步砍即可。

方法:bfs求点与点之间的最小代价,treeMap维护树高和对应位置的映射。

代码如下:

    public int cutOffTree(List<List<Integer>> forest) {
        int n = forest.size();
        if (n == 0) return 0;
        int m = forest.get(0).size();
        if (m == 0) return 0;
        int[][] map = new int[n][m];

        TreeMap<Integer, int[]> mem = new TreeMap<>();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                map[i][j] = forest.get(i).get(j);
                mem.put(map[i][j], new int[] {i, j});
            }
        }

        dist = new int[n][m];
        init();

        int ans = 0;
        Set<Integer> keys = mem.keySet();

        distMap(map, new int[] {0,0});
        for (int key : keys) {
            if (key > 1) {
                int[] now = mem.get(key);
                int v = dist[now[0]][now[1]];
                if (v == INF) return -1;
                ans += v;
                distMap(map, now);
            }
        }

        return ans;
    }

    static final int INF = 1 << 31;
    static final int[][] dir = {{1, 0},{-1, 0},{0, -1},{0, 1}};
    int[][] dist;

    public void init() {
        for (int i = 0; i < dist.length; ++i) Arrays.fill(dist[i], INF);
    }

    public void distMap(int[][] map, int[] start) {
        int n = map.length;
        int m = map[0].length;
        init();

        Queue<int[]> queue = new LinkedList<>();

        queue.offer(start);
        dist[start[0]][start[1]] = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; ++i) {
                int[] now = queue.poll();
                for (int[] d : dir) {
                    int nx = d[0] + now[0];
                    int ny = d[1] + now[1];
                    if (nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] >= 1 && dist[nx][ny] == INF) {
                        queue.offer(new int[] {nx, ny});
                        dist[nx][ny] = dist[now[0]][now[1]] + 1;
                    }
                }
            }
        }
    }

源点不确定,需要bfs多次,还有这操作。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值