前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >675. Cut Off Trees for Golf Event

675. Cut Off Trees for Golf Event

作者头像
用户1147447
发布2019-05-26 00:19:34
3780
发布2019-05-26 00:19:34
举报
文章被收录于专栏:机器学习入门机器学习入门

LWC 49: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维护树高和对应位置的映射。

代码如下:

代码语言:javascript
复制
    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多次,还有这操作。。。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年09月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • LWC 49:675. Cut Off Trees for Golf Event
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档