LWC 49:675. Cut Off Trees for Golf Event
来源:互联网 发布:linux socket多线程 编辑:程序博客网 时间:2024/06/05 18:41
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多次,还有这操作。。。
- LWC 49:675. Cut Off Trees for Golf Event
- 675. Cut Off Trees for Golf Event
- 675. Cut Off Trees for Golf Event
- LeetCode 675. Cut Off Trees for Golf Event
- LeetCode 675. Cut Off Trees for Golf Event
- leetcode675. Cut Off Trees for Golf Event(Hard)
- leetcode 675. Cut Off Trees for Golf Event 广度优先遍历BFS
- Link-Cut Trees 板子
- Golf
- 动态树 Link-Cut Trees
- 动态树之Link-Cut Trees
- BZOJ 1180 [CROATIAN2009]OTOCI Link Cut Trees
- BZOJ 3282 Tree link cut trees
- LWC 55:712. Minimum ASCII Delete Sum for Two Strings
- 一个英语笑话:Cut off my dog's tail
- 如何解决cut off中文字符(unicode)产生的乱码
- LWC 49:674. Longest Continuous Increasing Subsequence
- LWC 49:676. Implement Magic Dictionary
- c# 字符串截取
- SpringMVC框架
- 想跳槽到互联网行业,一线和新一线城市,有哪些特别有潜力的互联网公司呢?
- 确定你真的有价值判断
- c++基础之字符串的插入与删除
- LWC 49:675. Cut Off Trees for Golf Event
- 【数据结构】算法时间复杂度分析
- 联合权值——o(n)做法
- Turtlebot3入门手册之八:Realsense r200安装与测试
- 机器学习基本功
- C语言位域
- 【Android】获取webview文章的标题
- idea常用快捷键
- SpannableString文字操作