c++ priority queue + bfs solution with comments
\n\nclass Solution {\npublic:\n    int cutOffTree(vector<vector<int>>& forest) {\n        int rows = forest.size();\n        int cols = rows ? forest[0].size() 
```\n\nclass Solution {\npublic:\n    int cutOffTree(vector<vector<int>>& forest) {\n        int rows = forest.size();\n        int cols = rows ? forest[0].size() : 0;\n        \n        if (!rows || !cols) {\n            return 0;\n        }\n        \n        // Start location is blocked\n        if (forest[0][0] == 0) {\n            return -1;\n        }\n        \n        // Min queue of trees based on height\n         priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> trees;\n        \n        for (int r = 0; r < rows; ++r) {\n            for (int c = 0; c < cols; ++c) {\n                if (forest[r][c] > 1) {\n                   trees.push({forest[r][c], {r, c}}); \n                }\n            }\n        }\n        \n        // Legal moves\n        vector<pair<int, int>> moves = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n        \n        // Track visited\n        vector<vector<int>> visited(rows, vector<int>(cols, -1));\n        \n        // Total distance\n        int totalDist = 0;\n        \n        // Start location\n        pair<int, int> start = {0, 0};\n        \n        // Tree ID\n        int i = 0;\n        \n        // Iterate through trees in increasing height order\n        while (!trees.empty()) {\n            pair<int, int> to = trees.top().second;\n            trees.pop();\n            \n            // Level order traversal\n            bool found = false;\n            int dist = -1;  \n            queue<pair<int, int>> qu;\n            \n            qu.push({start.first, start.second});\n            visited[start.first][start.second] = i;\n            \n            while (!qu.empty() && !found) {\n                ++dist;\n                \n                for (int j = 0, r, c, size = qu.size(); j < size && !found; ++j) {\n                    tie(r, c) = qu.front();\n                    qu.pop();\n                \n                    // Break if target location found\n                    if (r == to.first && c == to.second) {\n                        found = true;\n                        break;\n                    }\n                \n                    for (int m = 0, nr, nc; m < 4; ++m) {\n                        // New location\n                        nr = r + moves[m].first;\n                        nc = c + moves[m].second;\n\n                        // Skip out of bound, blockages and already scheduled\n                        if (nr < 0 || nr >= rows || nc < 0 || nc >= cols || visited[nr][nc] == i || forest[nr][nc] == 0) {\n                            continue;\n                        }\n                            \n                        // Mark visited asap otherwise it will be scheduled mutiple times\n                        visited[nr][nc] = i;\n                        qu.push({nr, nc});\n                    }\n                }\n            }\n            \n            // Return if not found\n            if (!found) {\n                return -1;\n            }\n            \n            // Update total distance\n            totalDist += dist;\n            \n            // Update start\n            start = to;\n            \n            // Update id\n            ++i;\n        }\n        \n        return totalDist;\n    }\n};\n\n```
[]