1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <queue> #include <tuple> #include <vector>
using std::priority_queue; using std::tuple; using std::vector;
typedef priority_queue<tuple<int, int, int>, vector<tuple<int, int, int> >, std::greater<tuple<int, int, int> > > min_heap; class Solution { public: int trapRainWater(vector<vector<int> > &heightMap) { int m = heightMap.size(); int n = heightMap[0].size(); int res = 0; min_heap pq;
vector<vector<bool> > visit(m, vector<bool>(n, false));
if (m <= 2 || n <= 2) { return 0; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (i == 0 || i == m - 1 || j == 0 || j == n - 1) { pq.push({ heightMap[i][j], i, j }); visit[i][j] = true; } } }
int dirs[] = { -1, 0, 1, 0, -1 }; while (!pq.empty()) { auto [h, x, y] = pq.top(); pq.pop(); for (int k = 0; k < 4; ++k) { int nx = x + dirs[k]; int ny = y + dirs[k + 1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visit[nx][ny]) { if (heightMap[nx][ny] < h) { res += h - heightMap[nx][ny]; } visit[nx][ny] = true; pq.push({ std::max(heightMap[nx][ny], h), nx, ny }); } } }
return res; } };
|