{"id":274,"date":"2017-09-14T23:28:07","date_gmt":"2017-09-15T06:28:07","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=274"},"modified":"2018-07-10T18:28:59","modified_gmt":"2018-07-11T01:28:59","slug":"leetcode-675-cut-off-trees-for-golf-event","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-675-cut-off-trees-for-golf-event\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 675. Cut Off Trees for Golf Event"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 675. Cut Off Trees for Golf Event - \u5237\u9898\u627e\u5de5\u4f5c EP55\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/OFkLC30OxXM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/cut-off-trees-for-golf-event\/\">https:\/\/leetcode.com\/problems\/cut-off-trees-for-golf-event\/<\/a><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>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:<\/p>\n<ol>\n<li><code>0<\/code>\u00a0represents the\u00a0<code>obstacle<\/code>\u00a0can&#8217;t be reached.<\/li>\n<li><code>1<\/code>\u00a0represents the\u00a0<code>ground<\/code>\u00a0can be walked through.<\/li>\n<li><code>The place with number bigger than 1<\/code>\u00a0represents a\u00a0<code>tree<\/code>\u00a0can be walked through, and this positive number represents the tree&#8217;s height.<\/li>\n<\/ol>\n<p>You are asked to cut off\u00a0<b>all<\/b>\u00a0the trees in this forest in the order of tree&#8217;s height &#8211; always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).<\/p>\n<p>You will start from the point (0, 0) and you should output the minimum steps\u00a0<b>you need to walk<\/b>\u00a0to cut off all the trees. If you can&#8217;t cut off all the trees, output -1 in that situation.<\/p>\n<p>You are guaranteed that no two\u00a0<code>trees<\/code>\u00a0have the same height and there is at least one tree needs to be cut off.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \r\n[\r\n [1,2,3],\r\n [0,0,4],\r\n [7,6,5]\r\n]\r\nOutput: 6\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \r\n[\r\n [1,2,3],\r\n [0,0,0],\r\n [7,6,5]\r\n]\r\nOutput: -1\r\n<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"\">Input: \r\n[\r\n [2,3,4],\r\n [0,0,5],\r\n [8,7,6]\r\n]\r\nOutput: 6\r\nExplanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.\r\n<\/pre>\n<p><b>Hint<\/b>: size of the given matrix will not exceed 50&#215;50.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Greedy + Shortest path<\/p>\n<p>Identify and sort the trees by its heights, then find shortest paths between<\/p>\n<p>0,0 to tree[1]<br \/>\ntree[1] to tree[2]<br \/>\n&#8230;<br \/>\ntree[n-1] to tree[n]<\/p>\n<p>Time complexity: O(m^2n^2)<\/p>\n<p>Space complexity: O(mn)<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/675-ep55-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-282 size-full\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/675-ep55-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/675-ep55-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/675-ep55-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/675-ep55-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/675-ep55-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<pre class=\"lang:default decode:true  \">\/\/ Author: Huahua\r\n\/\/ Running time: 282 ms\t\r\nclass Solution {\r\npublic:\r\n    int cutOffTree(vector&lt;vector&lt;int&gt;&gt;&amp; forest) {    \r\n        m_ = forest.size();\r\n        n_ = forest[0].size();\r\n        \r\n        \/\/ {height, x, y}\r\n        vector&lt;tuple&lt;int,int,int&gt;&gt; trees;\r\n        for (int y = 0; y &lt; m_; ++y)\r\n            for (int x = 0; x &lt; n_; ++x)\r\n                if (forest[y][x] &gt; 1)\r\n                    trees.emplace_back(forest[y][x], x, y);\r\n        \r\n        \/\/ sort trees by height\r\n        sort(trees.begin(), trees.end());\r\n        \r\n        int sx = 0;\r\n        int sy = 0;\r\n        \r\n        int total_steps = 0;\r\n        \r\n        \/\/ Move from current position to next tree to cut\r\n        for (int i = 0; i &lt; trees.size(); ++i) {\r\n            int tx = get&lt;1&gt;(trees[i]);\r\n            int ty = get&lt;2&gt;(trees[i]);\r\n            \r\n            int steps = BFS(forest, sx, sy, tx, ty);\r\n            if (steps == INT_MAX) return -1;\r\n            \r\n            \/\/ Cut the tree, not necessary\r\n            forest[ty][tx] = 1;\r\n            \r\n            total_steps += steps;\r\n            \r\n            sx = tx;\r\n            sy = ty;\r\n        }\r\n        \r\n        return total_steps;\r\n        \r\n    }    \r\nprivate:\r\n    \/\/ min steps to go from (sx,sy) to (tx,ty) based on current map\r\n    \/\/ INT_MAX means not reachable\r\n    int BFS(const vector&lt;vector&lt;int&gt;&gt;&amp; forest, \r\n              int sx, int sy, \r\n              int tx, int ty) {\r\n        \r\n        static int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\r\n        \r\n        auto visited = vector&lt;vector&lt;int&gt;&gt;(m_, vector&lt;int&gt;(n_, 0));\r\n        \r\n        \/\/ {x, y}\r\n        queue&lt;pair&lt;int,int&gt;&gt; q;\r\n        q.emplace(sx, sy);\r\n        \r\n        int steps = 0;\r\n        while (!q.empty()) {\r\n            int new_nodes = q.size();\r\n            while (new_nodes--) {\r\n                auto node = q.front();\r\n                q.pop();\r\n                const int cx = node.first;\r\n                const int cy = node.second;\r\n                \r\n                \/\/ Found the shortest path\r\n                if (cx == tx &amp;&amp; cy == ty) \r\n                    return steps;\r\n                \r\n                for (int i = 0; i &lt; 4; ++i) {\r\n                    const int x = cx + dirs[i][0];\r\n                    const int y = cy + dirs[i][1];\r\n                    \r\n                    \/\/ Out of bound or unwalkable\r\n                    if (x &lt; 0 || x == n_ \r\n                     || y &lt; 0 || y == m_\r\n                     || !forest[y][x]\r\n                     || visited[y][x]) continue;\r\n                    \r\n                    \/\/ Mark x, y as visited\r\n                    visited[y][x] = 1;                    \r\n                    q.emplace(x, y);                \r\n                }\r\n            }\r\n            ++steps;\r\n        }\r\n        \r\n        \/\/ Impossible to reach\r\n        return INT_MAX;\r\n    }\r\n    \r\n    int m_;\r\n    int n_;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76,44],"tags":[34,88,87],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-graph","category-searching","tag-bfs","tag-greedy","tag-shortest-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":12,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":3057,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions\/3057"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}