-
题目链接:https://leetcode-cn.com/problems/pacific-atlantic-water-flow/
-
思路:
- 用深搜先找到能流入太平洋的位置,用一个数组储存
- 用深搜先找到能流入大西洋的位置,用一个数组储存
- 比较两个数组,找到既能流入太平洋又能流入大西洋的位置
-
代码:
static const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; class Solution { public: vector<vector<int>> heights; void dfs(int row, int col, vector<vector<bool>> & ocean) { int m = ocean.size(); int n = ocean[0].size(); if (ocean[row][col]) { return; } ocean[row][col] = true; for (int i = 0; i < 4; i++) { int newRow = row + dirs[i][0], newCol = col + dirs[i][1]; if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && heights[newRow][newCol] >= heights[row][col]) { dfs(newRow, newCol, ocean); } } } vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) { this->heights = heights; int m = heights.size(); int n = heights[0].size(); vector<vector<bool>> pacific(m, vector<bool>(n, false)); vector<vector<bool>> atlantic(m, vector<bool>(n, false)); for (int i = 0; i < m; i++) { dfs(i, 0, pacific); } for (int j = 1; j < n; j++) { dfs(0, j, pacific); } for (int i = 0; i < m; i++) { dfs(i, n - 1, atlantic); } for (int j = 0; j < n - 1; j++) { dfs(m - 1, j, atlantic); } vector<vector<int>> result; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (pacific[i][j] && atlantic[i][j]) { vector<int> cell; cell.emplace_back(i); cell.emplace_back(j); result.emplace_back(cell); } } } return result; } };
417.太平洋大西洋水流问题
最新推荐文章于 2024-07-17 20:43:45 发布