C++ Solution, Priority Queue + BFS
My C++ solution using priority queue and bfs.\n\n\nclass Solution {\npublic:\n    int cutOffTree(vector<vector<int>>& forest) \n    {\n        m = forest.size()
My C++ solution using priority queue and bfs.\n\n```\nclass Solution {\npublic:\n    int cutOffTree(vector<vector<int>>& forest) \n    {\n        m = forest.size();\n        if (m == 0) return -1;\n        n = forest[0].size();\n        \n        typedef tuple<int, int, int> HeightXY;\n        priority_queue<HeightXY, vector<HeightXY>, greater<HeightXY>> minHeap;\n        \n        for (int i = 0; i < m; i++)\n        {\n            for (int j = 0; j < n; j++)\n            {\n                if (forest[i][j] > 1) // contains tree\n                {\n                    minHeap.push({forest[i][j], i, j});\n                }\n            }\n        }\n        \n        int res = 0;\n        \n        // Starting from [0,0] find the min steps to reach 1st target from min heap top.\n        // Next start from this 1st target which becomes starting point for next iteration\n        // and find min steps to reach 2nd target from min heap (which is current top now).\n        // Continue until min heap is empty and accumulate these steps.\n        pair<int, int> start{0, 0};\n        pair<int, int> target;\n        int steps = 0;\n        while(!minHeap.empty())\n        {\n            auto cur = minHeap.top();\n            minHeap.pop();\n            \n            target = {get<1>(cur), get<2>(cur)}; // row, col\n            steps = bfs(forest, start, target);\n            \n            if (steps == -1) return -1;\n            \n            res += steps;\n            start = target;\n        }\n        \n        return res;\n    }\n    \n    int bfs(vector<vector<int>>& forest, pair<int, int>& start, pair<int, int>& target)\n    {\n        if (start.first == target.first && start.second == target.second)\n            return 0;\n        \n        vector<vector<bool>> visited(m, vector<bool>(n, false));\n        \n        queue<pair<int, int>> q;\n        q.push(start);\n        visited[start.first][start.second] = true;\n            \n        int steps = 0;\n        while (!q.empty())\n        {\n            steps++;\n            int size = q.size();\n            while (size--)\n            {\n                auto cur = q.front();\n                q.pop();\n                \n                for (auto dir : dirs)\n                {\n                    int x = cur.first + dir.first;\n                    int y = cur.second + dir.second;\n                    \n                    // forest[x][y] = 0 cannot be walked through\n                    if (x >= 0 && x < m && y >= 0 && y < n && forest[x][y] > 0 && !visited[x][y])\n                    {\n                        if (x == target.first && y == target.second)\n                            return steps;\n                        \n                        visited[x][y] = true;\n                        q.push({x, y});\n                    }\n                }\n            }\n        }\n        \n        return -1;\n    }\n    \nprivate:\n    int m;\n    int n;\n    vector<pair<int, int>> dirs = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };\n};\n```
['Breadth-First Search', 'C', 'Heap (Priority Queue)']