Pacific Atlantic Water Flow(leetcode)

    原题如下:

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

Note:

  1. The order of returned grid coordinates does not matter.
  2. Both m andn are less than 150.

Example:

Given the following 5x5 matrix:

  Pacific ~   ~   ~   ~   ~ 
       ~  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  *
          *   *   *   *   * Atlantic

Return:

[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
大意就是给定一个m*n的数组,数组中的任何一个数字只能走向上下左右与之相邻的数字上,而且不能走向比它大的数字,走到左边缘和上边缘就是到了太平洋,走到右边缘和下边缘就是到了大西洋,求既能到太平洋,又能到大西洋的数字的下标的集合。
基本思路(逆向求解):
   我们给这个数组维护两个bool类型的数组visited1和visited2,若visited1[i][j]=true,说明第i行,第j列的元素能够到达太平洋,若visited2[i][j]=true,说明第i行,第j列的元素能够到达大西洋,然后我们对边缘的元素进行DFS,进行逆向求解,若是周围元素大于等于该元素,则将比该元素大的元素的对应的visited数组上的值标记为true,然后以比该元素大的元素作为新的起点进行DFS,继续查找周围更大的元素,继续进行标记,当将所有的边缘元素均进行DFS后,该矩阵中所有能够到达太平洋的元素和所有能够到达大西洋的元素均被标记,之后遍历一下两个矩阵,找出所有的同时能到达太平洋和大西洋的元素的下标即可。
算法:
1、创建visited1,visited2两个bool数组和给定的数组大小一样,初值均为false;
2、对太平洋边缘的每个元素进行DFS,同时将边缘元素在visited1中的值标记为true,DFS找出所有比上一个元素大于或者等于的元素,标记为true;
3、对大西洋边缘的每个元素执行与2同样的操作,不过其标记是存在visited2中;
4、遍历visited1和visited2,找出所有既能到达太平洋,又能到达大西洋的元素的下标

具体代码如下:
class Solution {
public:
    int d1[4] = { 0,0,1,-1 };
 int d2[4] = { 1,-1,0,0 };
 void ReachPacific(vector<vector<int> >& m, vector<vector<bool> >& visited, int x, int y) {
  int n1 = m.size();
  int n2 = m[0].size();
  if (x == 0 || y == 0)
   visited[x][y] = true;
  for (int i = 0; i < 4; i++) {
   int temp_x = x + d1[i];
   int temp_y = y + d2[i];
   if (temp_x >= 0 && temp_x < n1&&temp_y >= 0 && temp_y < n2) {
    if (m[temp_x][temp_y] >= m[x][y] && visited[temp_x][temp_y] == false) {
     visited[temp_x][temp_y] = true;
     ReachPacific(m, visited, temp_x, temp_y);
    }
   }
  }
 }
 void ReachAtlantic(vector<vector<int> >& m, vector<vector<bool> >& visited, int x, int y) {
  int n1 = m.size();
  int n2 = m[0].size();
  if (x == n1-1 || y == n2-1)
   visited[x][y] = true;
  for (int i = 0; i < 4; i++) {
   int temp_x = x + d1[i];
   int temp_y = y + d2[i];
   if (temp_x >= 0 && temp_x < n1&&temp_y >= 0 && temp_y < n2) {
    if (m[temp_x][temp_y] >= m[x][y] && visited[temp_x][temp_y] == false) {
     visited[temp_x][temp_y] = true;
     ReachAtlantic(m, visited, temp_x, temp_y);
    }
   }
  }
 }
 vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
  int n1 = matrix.size();
  vector<pair<int, int> > res;
  if(n1==0)return res;
  int n2 = matrix[0].size();
  if(n2==0)return res;
  vector<bool> t1(n2, 0);
  vector <vector<bool> > visited1(n1, t1);//太平洋
  vector<vector<bool> > visited2(n1, t1);//大西洋
  for (int i = 0; i<n2; ++i) {
   //visited1[0][i] = 1;
   //visited[n1 - 1][i] = 1;
   ReachPacific(matrix, visited1, 0, i);
   ReachAtlantic(matrix, visited2, n1 - 1, i);
  }
  for (int i = 0; i < n1; i++) {
   ReachPacific(matrix, visited1, i, 0);
   ReachAtlantic(matrix, visited2, i, n2-1);
  }
  for (int i = 0; i < n1; ++i) {
   for (int j = 0; j < n2; ++j) {
    if (visited1[i][j] && visited2[i][j])
     res.push_back(make_pair(i, j));
   }
  }
  return res;
 }
};         

由于要对每个边缘元素都进行DFS,所以算法复杂度为O(N^3),应该可以改进的更好。                                                                                                        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值