You are given an n x n
integer matrix grid
where each value grid[i][j]
represents the elevation at that point (i, j)
.
It starts raining, and water gradually rises over time. At time t
, the water level is t
, meaning any cell with elevation less than equal to t
is submerged or reachable.
You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t
. You can swim infinite distances in zero time. Of course, you must stay within the boundaries of the grid during your swim.
Return the minimum time until you can reach the bottom right square (n - 1, n - 1)
if you start at the top left square (0, 0)
.
這週的每日一題比上次的三角型數學題難很多,有關 water flow,用最短路徑(SP) , minmax, dfs, bfs 解法的問題。
grid
,在時間 t 的水位也是 t,表示棋盤上數字 ≤ t 的位置可抵達n == grid.length, n == grid[i].length
1 <= n <= 50
0 <= grid[i][j] < n^2
grid[i][j]
is unique. 方陣的每個數字都是獨立的 _____
|0|2|
|1|3|
_____
Input: grid = [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0). 四個方向都到不了,t=1時可以到(1,0)高度≤1的格子能游,t=2時可以到(0,1)高度≤2的格子能游
You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.
class Solution {
public:
int swimInWater(vector<vector<int>>& grid) {
int n = grid.size();
vector<vector<int>> directions = {{-1,0}, {1,0}, {0,-1}, {0,1}};
// 上 下 左 右
// {高度, {行, 列}} - 最小堆
priority_queue<pair<int, pair<int,int>>,
vector<pair<int, pair<int,int>>>,
greater<>> pq;
//二維陣列
vector<vector<bool>> visited(n, vector<bool>(n, false));
pq.push({grid[0][0], {0, 0}});
visited[0][0] = true;
int maxHeight = 0;
while (!pq.empty()) {
auto [height, pos] = pq.top(); //取得頂端元素(不移除)
auto [row, col] = pos;
pq.pop();//先用top()取再移除
maxheight = max(maxheight, height);
// 到達終點
if (row == n-1 && col == n-1) {
return maxheight; // 第一次到達就是最優解
}
// 探索四個方向
for (auto& dir : directions) {
int newRow = row + dir[0];
int newCol = col + dir[1];
// 邊界檢查
if (newRow < 0 || newRow >= n ||
newCol < 0 || newCol >= n) {
continue;
}
if (visited[newRow][newCol]) {
continue;
}
visited[newRow][newCol] = true; //合法&未訪問
pq.push({grid[newRow][newCol], {newRow, newCol}});
}
}
return -1;
}
};
grid = [[0,2],
[1,3]]
Step 1: pq = [(0, {0,0})]
處理 (0,0),高度=0
加入鄰居:(1,0)高度1, (0,1)高度2
Step 2: pq = [(1, {1,0}), (2, {0,1})]
處理 (1,0),高度=1,maxHeight=1
加入鄰居:(1,1)高度3
Step 3: pq = [(2, {0,1}), (3, {1,1})]
處理 (0,1),高度=2,maxHeight=2
Step 4: pq = [(3, {1,1})]
處理 (1,1),高度=3,maxHeight=3
到達終點!返回 3
// 最小堆(小的在前)- 本題使用
priority_queue
pair<int, pair<int,int>>, // 元素類型
vector<pair<int, pair<int,int>>>, // 容器
greater<> // 比較器:最小堆
> pq;
// 操作
pq.push({5, {2, 3}}); // 插入 {高度5, 位置(2,3)}
auto top = pq.top(); // 取得最小元素(不移除)
pq.pop(); // 移除最小元素
bool empty = pq.empty(); // 檢查是否為空
----------------------------------------------------------
push(element) O(log n) 插入元素
top() O(1) 取得頂端元素(不移除)
pop() O(log n) 移除頂端元素
empty() O(1) 檢查是否為空
size() O(1) 取得元素數量
vector<vector<bool>> visited(n, vector<bool>(n, false));
// ↑ ↑ ↑ ↑
// 外層大小 內層類型 內層大小 初值
for (auto dir : directions) // 複製 4 次,慢
for (auto& dir : directions) // 不複製,快!
for (const auto& dir : ...) // 不複製 + 安全
ps. 部分內容經 AI 協助整理