【LeetCode】417. Pacific Atlantic Water Flow Add to List
2016-11-24 17:39
411 查看
题目:
Given an
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:
The order of returned grid coordinates does not matter.
Both m and n are less than 150.
Example:
分析:
这道题给了一个表示陆地高度的矩阵,高度高的水流可以流到高度低的地方,矩阵的上方和左方表示太平洋,下方和右方表示大西洋。要求出矩阵中的哪些位置既可以到达大西洋又可以到达太平洋。
我的想法是用两个向量来分别存储可以到达大西洋和到达太平洋的位置。通过宽度优先搜索对矩阵的四个边上的元素进行遍历,找到可以流入海洋的位置。然后找出两个向量中共同的元素。
代码:
class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
vector<pair<int,int>> toPacific;
vector<pair<int,int>> toAtlantic;
vector<pair<int,int>> result;
int m = matrix.size();
if(m == 0) return result;
int n = matrix[0].size();
int visited[150][150];
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
visited[i][j] = 0;
}
for(int i = 0;i<m;i++)
{
if(visited[i][0]!=1)
{
toPacific.push_back(pair<int,int>(i,0));
visited[i][0] = 1;
rec(toPacific,visited,matrix,i,0);
}
}
for(int i = 1;i<n;i++)
{
if(visited[0][i] != 1)
{
toPacific.push_back(pair<int,int>(0,i));
visited[0][i] = 1;
rec(toPacific,visited,matrix,0,i);
}
}
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
visited[i][j] = 0;
}
for(int i = 0;i<m;i++)
{
if(visited[i][n-1] == 0)
{
toAtlantic.push_back(pair<int,int>(i,n-1));
visited[i][n-1] = 1;
rec(toAtlantic,visited,matrix,i,n-1);
}
}
for(int i = 0;i<n;i++)
{
if(visited[m-1][i] == 0)
{
toAtlantic.push_back(pair<int,int>(m-1,i));
visited[m-1][i] = 1;
rec(toAtlantic,visited,matrix,m-1,i);
}
}
vector<pair<int,int>>::iterator search;
for(int i = 0;i<toPacific.size();i++)
{
search=find(toAtlantic.begin(),toAtlantic.end(),toPacific[i]);
if(search!=toAtlantic.end())
result.push_back(toPacific[i]);
}
return result;
}
void rec(vector<pair<int,int>>& to,int visited[150][150],vector<vector<int>>& matrix,int a,int b)
{
int m = matrix.size();
int n = matrix[0].size();
queue<pair<int,int>> q;
q.push(pair<int,int>(a,b));
while(!q.empty())
{
int f = q.front().first;
int s = q.front().second;
q.pop();
for(int i = -1;i<=1;i++)
{
for(int j = -1;j<=1;j++)
{
if(i!=0&&j!=0) continue;
if(f+i>=0&&s+j>=0&f+i<m&&s+j<n)
{
if(matrix[f+i][s+j]>=matrix[f][s]&&visited[f+i][s+j]!=1)
{
q.push(pair<int,int>(f+i,s+j));
to.push_back(pair<int,int>(f+i,s+j));
visited[f+i][s+j] = 1;
}
}
}
}
}
}
};
Given an
m x nmatrix 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:
The order of returned grid coordinates does not matter.
Both m and n 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).
分析:
这道题给了一个表示陆地高度的矩阵,高度高的水流可以流到高度低的地方,矩阵的上方和左方表示太平洋,下方和右方表示大西洋。要求出矩阵中的哪些位置既可以到达大西洋又可以到达太平洋。
我的想法是用两个向量来分别存储可以到达大西洋和到达太平洋的位置。通过宽度优先搜索对矩阵的四个边上的元素进行遍历,找到可以流入海洋的位置。然后找出两个向量中共同的元素。
代码:
class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
vector<pair<int,int>> toPacific;
vector<pair<int,int>> toAtlantic;
vector<pair<int,int>> result;
int m = matrix.size();
if(m == 0) return result;
int n = matrix[0].size();
int visited[150][150];
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
visited[i][j] = 0;
}
for(int i = 0;i<m;i++)
{
if(visited[i][0]!=1)
{
toPacific.push_back(pair<int,int>(i,0));
visited[i][0] = 1;
rec(toPacific,visited,matrix,i,0);
}
}
for(int i = 1;i<n;i++)
{
if(visited[0][i] != 1)
{
toPacific.push_back(pair<int,int>(0,i));
visited[0][i] = 1;
rec(toPacific,visited,matrix,0,i);
}
}
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
visited[i][j] = 0;
}
for(int i = 0;i<m;i++)
{
if(visited[i][n-1] == 0)
{
toAtlantic.push_back(pair<int,int>(i,n-1));
visited[i][n-1] = 1;
rec(toAtlantic,visited,matrix,i,n-1);
}
}
for(int i = 0;i<n;i++)
{
if(visited[m-1][i] == 0)
{
toAtlantic.push_back(pair<int,int>(m-1,i));
visited[m-1][i] = 1;
rec(toAtlantic,visited,matrix,m-1,i);
}
}
vector<pair<int,int>>::iterator search;
for(int i = 0;i<toPacific.size();i++)
{
search=find(toAtlantic.begin(),toAtlantic.end(),toPacific[i]);
if(search!=toAtlantic.end())
result.push_back(toPacific[i]);
}
return result;
}
void rec(vector<pair<int,int>>& to,int visited[150][150],vector<vector<int>>& matrix,int a,int b)
{
int m = matrix.size();
int n = matrix[0].size();
queue<pair<int,int>> q;
q.push(pair<int,int>(a,b));
while(!q.empty())
{
int f = q.front().first;
int s = q.front().second;
q.pop();
for(int i = -1;i<=1;i++)
{
for(int j = -1;j<=1;j++)
{
if(i!=0&&j!=0) continue;
if(f+i>=0&&s+j>=0&f+i<m&&s+j<n)
{
if(matrix[f+i][s+j]>=matrix[f][s]&&visited[f+i][s+j]!=1)
{
q.push(pair<int,int>(f+i,s+j));
to.push_back(pair<int,int>(f+i,s+j));
visited[f+i][s+j] = 1;
}
}
}
}
}
}
};
相关文章推荐
- Leetcode-417. Pacific Atlantic Water Flow
- Leetcode 417. Pacific Atlantic Water Flow 流向海洋 解题报告
- 【Leetcode】417. Pacific Atlantic Water Flow
- [leetcode]417. Pacific Atlantic Water Flow
- LeetCode - 417 - Pacific Atlantic Water Flow
- leetcode 417. Pacific Atlantic Water Flow可以到达两边的点+经典DFS深度优先遍历做法
- [Leetcode] 417. Pacific Atlantic Water Flow 解题报告
- 第四周:( LeetCode417 ) Pacific Atlantic Water Flow(c++)
- LeetCode 417. Pacific Atlantic Water Flow
- leetcode 417. Pacific Atlantic Water Flow
- LeetCode 417. Pacific Atlantic Water Flow 题解
- [LeetCode] 417. Pacific Atlantic Water Flow 解题报告
- leetcode 417. Pacific Atlantic Water Flow
- 417. Pacific Atlantic Water Flow
- Leetcode: Pacific Atlantic Water Flow
- 417. Pacific Atlantic Water Flow
- 417. Pacific Atlantic Water Flow
- 417. Pacific Atlantic Water Flow
- 417. Pacific Atlantic Water Flow
- 417. Pacific Atlantic Water Flow