BFS
题目描述:
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 *
* * * * * AtlanticReturn:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions
with parentheses in above matrix).
为了方便,可以将问题反向求解。需要输出水可以流入两洋的位置,那么该位置的水分别至少有一条路径高度递减,通向左上边和右下边。而我们假设水是从下往上攀爬的,也就是要输出从左边、上边开始和从右边、下边开始攀爬,都可以到达的位置。所以从边缘开始进行两次BFS,若四周有大于等于当前高度,且未被访问过的位置,则将其加入搜索队列。若第二次搜索到达了前一次到达过的位置,则将位置加入结果中。
具体代码:
class Solution {
public:
vector<pair<int, int> > pacificAtlantic(vector< vector<int> >& matrix) {
vector<pair<int, int> > result;
int row = matrix.size();
if (row == 0) return result;
int col = matrix[0].size();
if (col == 0) return result;
queue< pair<int, int> > pa;
queue< pair<int, int> > at;
vector< bool > v;
for (int i = 0; i < col; i++)
v.push_back(false);
for (int i = 0; i < row; i++) {
flowedpa.push_back(v);
flowedat.push_back(v);
}
for (int i = 0; i < row; i++) {
flowedpa[i][0] = true;
pa.push(make_pair(i, 0));
flowedat[i][col-1] = true;
at.push(make_pair(i, col-1));
}
for (int i = 0; i < col; i++) {
flowedpa[0][i] = true;
if (i != 0) pa.push(make_pair(0, i));
flowedat[row-1][i] = true;
if (i != col-1) at.push(make_pair(row-1, i));
}
pair<int, int> tmp;
while (pa.size() != 0) {
tmp = pa.front();
pa.pop();
if (tmp.first < row-1 && flowedpa[tmp.first+1][tmp.second] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first+1][tmp.second]) {
pa.push(make_pair(tmp.first+1, tmp.second));
flowedpa[tmp.first+1][tmp.second] = true;
}
if (tmp.second < col-1 && flowedpa[tmp.first][tmp.second+1] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first][tmp.second+1]) {
pa.push(make_pair(tmp.first, tmp.second+1));
flowedpa[tmp.first][tmp.second+1] = true;
}
if (tmp.first > 0 && flowedpa[tmp.first-1][tmp.second] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first-1][tmp.second]) {
pa.push(make_pair(tmp.first-1, tmp.second));
flowedpa[tmp.first-1][tmp.second] = true;
}
if (tmp.second > 0 && flowedpa[tmp.first][tmp.second-1] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first][tmp.second-1]) {
pa.push(make_pair(tmp.first, tmp.second-1));
flowedpa[tmp.first][tmp.second-1] = true;
}
}
while (at.size() != 0) {
tmp = at.front();
at.pop();
if (flowedpa[tmp.first][tmp.second]) {
result.push_back(tmp);
}
if (tmp.first < row-1 && flowedat[tmp.first+1][tmp.second] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first+1][tmp.second]) {
at.push(make_pair(tmp.first+1, tmp.second));
flowedat[tmp.first+1][tmp.second] = true;
}
if (tmp.second < col-1 && flowedat[tmp.first][tmp.second+1] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first][tmp.second+1]) {
at.push(make_pair(tmp.first, tmp.second+1));
flowedat[tmp.first][tmp.second+1] = true;
}
if (tmp.first > 0 && flowedat[tmp.first-1][tmp.second] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first-1][tmp.second]) {
at.push(make_pair(tmp.first-1, tmp.second));
flowedat[tmp.first-1][tmp.second] = true;
}
if (tmp.second > 0 && flowedat[tmp.first][tmp.second-1] == false
&& matrix[tmp.first][tmp.second] <= matrix[tmp.first][tmp.second-1]) {
at.push(make_pair(tmp.first, tmp.second-1));
flowedat[tmp.first][tmp.second-1] = true;
}
}
return result;
}
private:
vector< vector< bool > > flowedpa;
vector< vector< bool > > flowedat;
};
(有很多重复的部分可以被简化)
113 / 113 test cases passed.
Status: Accepted
Runtime: 46 ms
Your runtime beats 67.89 % of cpp submissions.