417. Pacific Atlantic Water Flow

The description of problem

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. 
The Pacific Ocean touches the island's left and top edges, 
and the Atlantic Ocean touches the island's right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix 
heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, 
and west if the neighboring cell's height is less than or equal to the current cell's height. 
Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that 
rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pacific-atlantic-water-flow

Chinese version

有一个 m × n 的矩形岛屿,与 太平洋 和 大西洋 相邻。 “太平洋” 处于大陆的左边界和上边界,而 “大西洋” 处于大陆的右边界和下边界。

这个岛被分割成一个由若干方形单元格组成的网格。给定一个 m x n 的整数矩阵 heights , heights[r][c] 表示坐标 (r, c) 上单元格 高于海平面的高度 。

岛上雨水较多,如果相邻单元格的高度 小于或等于 当前单元格的高度,雨水可以直接向北、南、东、西流向相邻单元格。水可以从海洋附近的任何单元格流入海洋。

返回网格坐标 result 的 2D 列表 ,其中 result[i] = [ri, ci] 表示雨水从单元格 (ri, ci) 流动 既可流向太平洋也可流向大西洋 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pacific-atlantic-water-flow

An example
在这里插入图片描述

Solution 1: Depth first search

The codes

#include <vector>
#include <iostream>
using namespace std;
const static vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
class Solution {
public:
    vector<vector<int>> heights;
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights){
        this->heights = heights;
        int m = heights.size();
        int n = heights[0].size();
        vector<vector<int>> pacific(m, vector<int>(n, 0));
        vector<vector<int>> altantic(m, vector<int>(n, 0));
        for(int i = 0; i < m; i++){
            dfs(i, 0, pacific);
            dfs(i, n-1, altantic);
        }
        for(int j = 0; j < n; j++){
            dfs(0, j, pacific);
            dfs(m-1, j, altantic);
        }
        vector<vector<int>> res;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(pacific[i][j] == 1 && altantic[i][j] == 1){
                    res.emplace_back(vector<int>{i, j});
                }
            }
        }
        return res;
    }
    void dfs(int row, int col, vector<vector<int>>& visited){
        if(visited[row][col] == 1) return;
        visited[row][col] = 1;
        for(auto dir : dirs){
            int r = row + dir[0];
            int c = col + dir[1];
            if(r < 0 || r >= visited.size() || c < 0 || c >= visited[0].size()) continue;
            if(heights[row][col] <= heights[r][c]){
                dfs(r, c, visited);
            }
        }
    }  
};

int main()
{
    vector<vector<int>> heights = {{1,2,2,3,5},{3,2,3,4,4},{2,4,5,3,1},{6,7,1,4,5},{5,1,1,2,4}};
    Solution s;
    auto res = s.pacificAtlantic(heights);
    std::cout << "The elements in the grid satisfing the condition are: " << std::endl;
    for(auto v : res){
        cout << v[0] << " " << v[1] << endl;
    }
    return 0;
}

The corresponding results for depth first search

The elements in the grid satisfing the condition are:
0 4
1 3
1 4
2 2
3 0
3 1
4 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值