<p>You are a hiker preparing for an upcoming hike. You are given <code>heights</code>, a 2D array of size <code>rows x columns</code>, where <code>heights[row][col]</code> represents the height of cell <code>(row, col)</code>. You are situated in the top-left cell, <code>(0, 0)</code>, and you hope to travel to the bottom-right cell, <code>(rows-1, columns-1)</code> (i.e.,&nbsp;<strong>0-indexed</strong>). You can move <strong>up</strong>, <strong>down</strong>, <strong>left</strong>, or <strong>right</strong>, and you wish to find a route that requires the minimum <strong>effort</strong>.</p>

<p>A route&#39;s <strong>effort</strong> is the <strong>maximum absolute difference</strong><strong> </strong>in heights between two consecutive cells of the route.</p>

<p>Return <em>the minimum <strong>effort</strong> required to travel from the top-left cell to the bottom-right cell.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex1.png" style="width: 300px; height: 300px;" /></p>

<pre>
<strong>Input:</strong> heights = [[1,2,2],[3,8,2],[5,3,5]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
</pre>

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

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex2.png" style="width: 300px; height: 300px;" /></p>

<pre>
<strong>Input:</strong> heights = [[1,2,3],[3,8,4],[5,3,5]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex3.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> This route does not require any effort.
</pre>

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

<ul>
	<li><code>rows == heights.length</code></li>
	<li><code>columns == heights[i].length</code></li>
	<li><code>1 &lt;= rows, columns &lt;= 100</code></li>
	<li><code>1 &lt;= heights[i][j] &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def minimumEffortPath(self, heights: List[List[int]]) -> int:
        # effort: 每條路徑中相鄰 cell 的最大差值
        # 找到有最小 effort 的那條路徑
        # 並回傳該路徑的 effort

        # 方向向量，表示上下左右四個方向
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        
        # 行列
        rows, cols = len(heights), len(heights[0])
        
        # 優先隊列，存儲 (當前路徑的最大高度差, 當前行, 當前列)
        pq = [(0, 0, 0)]  # 初始位置，(effort, row, col)
        
        # 記錄每個點的最小 effort
        effort = [[float('inf')] * cols for _ in range(rows)]
        effort[0][0] = 0
        
        while pq:
            cur_effort, x, y = heapq.heappop(pq)
            
            # 如果到達終點，直接返回最小 effort
            if x == rows - 1 and y == cols - 1:
                return cur_effort
            
            # 擴展當前點的四個鄰居
            for dx, dy in directions:
                nx, ny = x + dx, y + dy
                if 0 <= nx < rows and 0 <= ny < cols:
                    # 計算新的 effort
                    next_effort = max(cur_effort, abs(heights[nx][ny] - heights[x][y]))
                    # 如果找到更小的 effort，則更新並加入優先隊列
                    if next_effort < effort[nx][ny]:
                        effort[nx][ny] = next_effort
                        heapq.heappush(pq, (next_effort, nx, ny))
        
        return 0  # 這裡實際上不會執行

        """ TLE
        ways = [-1, 0, 1, 0, -1]

        def dfs(w, h, i, j, path, ans):
            # print(path)
            if i == h - 1 and j == w - 1:
                path.append((i, j))

                effort = 0
                for i in range(len(path) - 1):
                    a = heights[path[i][0]][path[i][1]]
                    b = heights[path[i + 1][0]][path[i + 1][1]]
                    if abs(a - b) > effort:
                        effort = abs(a - b)
                
                if effort < ans[0]:
                    ans[0] = effort

                path.pop()
                
                return
            
            path.append((i, j))

            for k in range(4):
                new_i, new_j = i + ways[k], j + ways[k + 1]
                if new_i >= 0 and new_i < h and new_j >= 0 and new_j < w \
                    and (new_i, new_j) not in path \
                    and abs(heights[i][j] - heights[new_i][new_j]) <= ans[0]:

                    dfs(w, h, new_i, new_j, path, ans)

            path.pop()

        
        w, h = len(heights[0]), len(heights)

        path = []
        ans = [float('inf')]
        dfs(w, h, 0, 0, path, ans)

        return ans[0]
        """


18000