Python solution with detailed explanation
Cut Off Trees for Golf Event https://leetcode.com/problems/cut-off-trees-for-golf-event/description/\n\nBFS based solution (in Python gives TLE)\n Use a priorit
**Cut Off Trees for Golf Event** https://leetcode.com/problems/cut-off-trees-for-golf-event/description/\n\n**BFS based solution (in Python gives TLE)**\n* Use a priority queue to arrange all trees in ascending height order.\n* Then simply use BFS to find the minimum steps between consecutive trees.\n* Time complexity: O(m^2 * n^2)\n```\nfrom heapq import heappop, heappush\nfrom collections import deque\n\nclass Solution:\n    def build_pq(self, forest):\n        pq = []\n        for i in range(len(forest)):\n            for j in range(len(forest[0])):\n                if forest[i][j] > 1:\n                    heappush(pq, (forest[i][j], i, j))\n        return pq\n    \n    def process_level(self, forest, dq, seen, xdest, ydest):\n        M,N = len(forest), len(forest[0])\n        for _ in range(len(dq)):\n            x2, y2 = dq.popleft()\n            if x2 == xdest and y2 == ydest:\n                return True\n            for xn,yn in ((x2-1,y2),(x2+1,y2),(x2,y2-1),(x2,y2+1)):\n                if 0<=xn<M and 0<=yn<N and forest[xn][yn] != 0 and (xn,yn) not in seen:\n                    dq.append((xn,yn))\n                    seen.add((xn,yn))\n        return False\n    \n    def find_steps_bfs(self, forest, start, dest):\n        dq = deque()\n        dq.append((start[0], start[1]))\n        seen = set()\n        steps = 0\n        while dq:\n            found = self.process_level(forest, dq, seen, dest[0], dest[1])\n            if found:\n                break\n            else:\n                steps += 1\n        return steps if found else -1\n    \n    def cutOffTree(self, forest):\n        """\n        :type forest: List[List[int]]\n        :rtype: int\n        """\n        pq = self.build_pq(forest)\n        xstart, ystart, steps = 0, 0, 0\n        while pq:\n            ht, xdest, ydest = heappop(pq)\n            curr_step = self.find_steps_bfs(forest, (xstart, ystart), (xdest, ydest))\n            if curr_step == -1:\n                return -1\n            else:\n                steps += curr_step\n            xstart, ystart = xdest, ydest\n        return steps    \n```
[]