1. 题目描述
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:
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).
2. 代码(最后一个case超时了)
leetcode上可AC的solution(BFS):参考
class Solution {
public:
vector<pair<int, int> > pacificAtlantic(vector<vector<int> >& matrix) {
int rows = matrix.size();
int cols = 0;
vector<pair<int, int> > res;
if(rows > 0) cols = matrix[0].size();
else return res;
// initialized 0; 1:can reach both
vector<vector<int> > canReached(rows,vector<int>(cols,0));
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
bfs(matrix, canReached, i, j);
if(canReached[i][j] == 1) {
res.push_back(make_pair(i,j));
}
}
}
return res;
}
void bfs(vector<vector<int> >& matrix, vector<vector<int> >& canReached, int i, int j) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<vector<bool> > visited(rows,vector<bool>(cols,false));
queue<pair<int, int> > bfs;
bfs.push(make_pair(i,j));
visited[i][j] = true;
pair<int, int> tmp;
bool ok1 = false, ok2 = false;
if((i == 0 || j == 0)) {
ok1 = true;
}
if(i == rows-1||j == cols-1) {
ok2 = true;
}
if(ok1 && ok2) {
canReached[i][j] = 1;
return;
}
while(!bfs.empty()) {
int size = bfs.size();
for(int h = 0; h < size; h++) {
tmp = bfs.front();
bfs.pop();
vector<pair<int, int> > next = canMove(matrix, visited, tmp.first, tmp.second);
for(int k = 0; k < next.size(); k++) {
pair<int,int> node = next[k];
if(node.first == 0 ||node.second == 0) {
ok1 = true;
}
if(node.first == rows-1 || node.second == cols-1) {
ok2 = true;
}
if(ok1 && ok2) {
canReached[i][j] = 1;
return;
} else {
visited[node.first][node.second] = true;
bfs.push(node);
}
}
}
}
return;
}
vector<pair<int, int> > canMove(vector<vector<int> >& matrix, vector<vector<bool> >& visited, int i, int j) {
vector<pair<int, int> > res;
int rows = matrix.size();
int cols = matrix[0].size();
if(i-1 >= 0 && !visited[i-1][j] && matrix[i-1][j] <= matrix[i][j]) {
res.push_back(make_pair(i-1,j));
}
if(i+1 < rows && !visited[i+1][j] && matrix[i+1][j] <= matrix[i][j]) {
res.push_back(make_pair(i+1, j));
}
if(j-1 >= 0 && !visited[i][j-1] && matrix[i][j-1] <= matrix[i][j]) {
res.push_back(make_pair(i,j-1));
}
if(j+1 < cols && !visited[i][j+1] && matrix[i][j+1] <= matrix[i][j]) {
res.push_back(make_pair(i, j+1));
}
return res;
}
};
能解决超时的朋友,麻烦联系我,谢谢~
联系邮箱:sysuygm@163.com