一、题目
二、题解
统计两个洋的水分别可以到哪些地方,然后再统计都能到达的地方
class Solution {
public:
int n, m, g[210][210], gg[210][210], check[210][210], dx[4]{0, 0, 1, -1}, dy[4]{1, -1, 0, 0};
void dfs(int i, int j, vector<vector<int>>& he, int c) {
check[i][j] = 1;
if (c) g[i][j]++; else gg[i][j]++;
for (int k = 0; k < 4; k++)
if (i + dx[k] >= 0 && i + dx[k] < n && j + dy[k] >= 0 && j + dy[k] < m && !check[i + dx[k]][j + dy[k]] && he[i][j] <= he[i + dx[k]][j + dy[k]])
dfs(i + dx[k], j + dy[k], he, c);
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
n = heights.size(), m = heights[0].size();
for (int i = 0; i < n; i++)
if (!check[i][0])
dfs(i, 0, heights, 0);
for (int j = 0; j < m; j++)
if (!check[0][j])
dfs(0, j, heights, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
check[i][j] = 0;
for (int i = 0; i < n; i++)
if (!check[i][m - 1])
dfs(i, m - 1, heights, 1);
for (int j = 0; j < m; j++)
if (!check[n - 1][j])
dfs(n - 1, j, heights, 1);
vector<vector<int>> res;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (g[i][j] && gg[i][j])
res.push_back({i, j});
return res;
}
};