https://leetcode.com/problems/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 theobstacle
can’t be reached.1
represents theground
can be walked through.The place with number bigger than 1
represents atree
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:
1 2 3 4 5 6 7 |
Input: [ [1,2,3], [0,0,4], [7,6,5] ] Output: 6 |
Example 2:
1 2 3 4 5 6 7 |
Input: [ [1,2,3], [0,0,0], [7,6,5] ] Output: -1 |
Example 3:
1 2 3 4 5 6 7 8 |
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. |
Hint: size of the given matrix will not exceed 50×50.
Idea:
Greedy + Shortest path
Identify and sort the trees by its heights, then find shortest paths between
0,0 to tree[1]
tree[1] to tree[2]
…
tree[n-1] to tree[n]
Time complexity: O(m^2n^2)
Space complexity: O(mn)
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
// Author: Huahua // Running time: 282 ms class Solution { public: int cutOffTree(vector<vector<int>>& forest) { m_ = forest.size(); n_ = forest[0].size(); // {height, x, y} vector<tuple<int,int,int>> trees; for (int y = 0; y < m_; ++y) for (int x = 0; x < n_; ++x) if (forest[y][x] > 1) trees.emplace_back(forest[y][x], x, y); // sort trees by height sort(trees.begin(), trees.end()); int sx = 0; int sy = 0; int total_steps = 0; // Move from current position to next tree to cut for (int i = 0; i < trees.size(); ++i) { int tx = get<1>(trees[i]); int ty = get<2>(trees[i]); int steps = BFS(forest, sx, sy, tx, ty); if (steps == INT_MAX) return -1; // Cut the tree, not necessary forest[ty][tx] = 1; total_steps += steps; sx = tx; sy = ty; } return total_steps; } private: // min steps to go from (sx,sy) to (tx,ty) based on current map // INT_MAX means not reachable int BFS(const vector<vector<int>>& forest, int sx, int sy, int tx, int ty) { static int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; auto visited = vector<vector<int>>(m_, vector<int>(n_, 0)); // {x, y} queue<pair<int,int>> q; q.emplace(sx, sy); int steps = 0; while (!q.empty()) { int new_nodes = q.size(); while (new_nodes--) { auto node = q.front(); q.pop(); const int cx = node.first; const int cy = node.second; // Found the shortest path if (cx == tx && cy == ty) return steps; for (int i = 0; i < 4; ++i) { const int x = cx + dirs[i][0]; const int y = cy + dirs[i][1]; // Out of bound or unwalkable if (x < 0 || x == n_ || y < 0 || y == m_ || !forest[y][x] || visited[y][x]) continue; // Mark x, y as visited visited[y][x] = 1; q.emplace(x, y); } } ++steps; } // Impossible to reach return INT_MAX; } int m_; int n_; }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment