<p>You are asked to cut off all the trees in a forest for a golf event. The forest is represented as an <code>m x n</code> matrix. In this matrix:</p>

<ul>
	<li><code>0</code> means the cell cannot be walked through.</li>
	<li><code>1</code> represents an empty cell that can be walked through.</li>
	<li>A number greater than <code>1</code> represents a tree in a cell that can be walked through, and this number is the tree&#39;s height.</li>
</ul>

<p>In one step, you can walk in any of the four directions: north, east, south, and west. If you are standing in a cell with a tree, you can choose whether to cut it off.</p>

<p>You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value at its cell becomes <code>1</code> (an empty cell).</p>

<p>Starting from the point <code>(0, 0)</code>, return <em>the minimum steps you need to walk to cut off all the trees</em>. If you cannot cut off all the trees, return <code>-1</code>.</p>

<p><strong>Note:</strong> The input is generated such that no two trees have the same height, and there is at least one tree needs to be cut off.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/trees1.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> forest = [[1,2,3],[0,0,4],[7,6,5]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> Following the path above allows you to cut off the trees from shortest to tallest in 6 steps.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/trees2.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> forest = [[1,2,3],[0,0,0],[7,6,5]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> The trees in the bottom row cannot be accessed as the middle row is blocked.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> forest = [[2,3,4],[0,0,5],[8,7,6]]
<strong>Output:</strong> 6
<b>Explanation:</b> You can follow the same path as Example 1 to cut off all the trees.
Note that you can cut off the first tree at (0, 0) before making any steps.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == forest.length</code></li>
	<li><code>n == forest[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 50</code></li>
	<li><code>0 &lt;= forest[i][j] &lt;= 10<sup>9</sup></code></li>
	<li>Heights of all trees are <strong>distinct</strong>.</li>
</ul>

class Solution:
    def get_adjacents(self, vertex, row_size, col_size):
        directions = [[0,1],[1,0],[0,-1],[-1,0]]
        adj_vertices = []
        for direction in directions:
            new_row = vertex[0] + direction[0]
            new_col = vertex[1] + direction[1]
            if new_row >= 0 and new_row < row_size and new_col >= 0 and new_col < col_size:
                adj_vertices.append((new_row, new_col))
        return adj_vertices

    def find_distance(self, src_vertex, destn_vertex, forest):
        queue = deque()
        visited = set()
        queue.append((src_vertex, 0))
        visited.add(src_vertex)
        while queue:
            curr_node, curr_distance = queue.popleft()
            
            if curr_node == destn_vertex:
                forest[curr_node[0]][curr_node[1]] = 1
                return curr_distance
            adj_vertices = self.get_adjacents(curr_node, len(forest), len(forest[0]))
            
            for adj_vertex in adj_vertices:                
                if adj_vertex not in visited and forest[adj_vertex[0]][adj_vertex[1]] != 0:
                    queue.append((adj_vertex, curr_distance + 1))
                    visited.add(adj_vertex)
            
        return -1


    def cutOffTree(self, forest: List[List[int]]) -> int:
        '''
        I assumed that I can take only one step at a time and I missed the fact that should I can
        leave a tree without cutting
        '''
        tree_indices = []
        sorted_height_vertices = []
        for row in range(len(forest)):
            for col in range(len(forest[0])):
                if forest[row][col] > 1:
                    heapq.heappush(tree_indices, (forest[row][col], row, col))                          
        
        
        curr_start = (0,0)
        tree_index = 0
        curr_distance = 0
        while tree_indices:
            tree_height, curr_destn_row, curr_destn_col = heapq.heappop(tree_indices)
            print (curr_destn_row)
            print (curr_destn_col)
            distance = self.find_distance(curr_start, (curr_destn_row, curr_destn_col), forest)
            if distance == -1:
                return -1
            curr_distance += distance
            curr_start = (curr_destn_row, curr_destn_col)
            tree_index += 1
        return curr_distance


        


        
17700