Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間10月前 (2025/01/22 22:16), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1299/1552 (看更多)
1765. Map of Highest Peak ## 思路 水是0, 相鄰的點最多差1 所以從水開始BFS ## Code ```cpp class Solution { public: vector<vector<int>> highestPeak(vector<vector<int>>& isWater) { int lenR = isWater.size(), lenC = isWater[0].size(); vector<pair<int, int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; vector<vector<int>> height(lenR, vector<int>(lenC, -1)); queue<pair<int, int>> q; for (int r=0; r<lenR; ++r) { for (int c=0; c<lenC; ++c) { if (isWater[r][c]) { q.push({r, c}); height[r][c] = 0; } } } while (!q.empty()) { auto [r, c] = q.front(); q.pop(); for (auto [dr, dc] : dirs) { int nextR = r + dr, nextC = c + dc; if (0 <= nextR && nextR < lenR && 0 <= nextC && nextC < lenC && height[nextR][nextC] == -1) { q.push({nextR, nextC}); height[nextR][nextC] = height[r][c] + 1; } } } return height; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 86.48.13.234 (日本) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1737555368.A.748.html
文章代碼(AID): #1daFseT8 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1daFseT8 (Marginalman)