733. Flood Fill
An image
is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
Given a coordinate (sr, sc)
representing the starting pixel (row and column) of the flood fill, and a pixel value newColor
, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
At the end, return the modified image.
Example 1:
Input: image = [[1,1,1],[1,1,0],[1,0,1]] sr = 1, sc = 1, newColor = 2 Output: [[2,2,2],[2,2,0],[2,0,1]] Explanation: From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected by a path of the same color as the starting pixel are colored with the new color. Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
Note:
The length ofimage
and
image[0]
will be in the range
[1, 50]
.The given starting pixel will satisfy
0 <= sr < image.length
and
0 <= sc < image[0].length
.The value of each color in
image[i][j]
and
newColor
will be an integer in
[0, 65535]
.
输入: image = [[1,1,1],[1,1,0],[1,0,1]] sr = 1, sc = 1, newColor = 2 输出: [[2,2,2],[2,2,0],[2,0,1]] 解析: 在图像的正中间,(坐标(sr,sc)=(1,1)), 在路径上所有符合条件的像素点的颜色都被更改成2。 注意,右下角的像素没有更改为2, 因为它不是在上下左右四个方向上与初始点相连的像素点。
典型的BFS或者DFS二维遍历。
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor)
{
if (image[sr][sc] == newColor) return image;
int row = image.size();
int col = image[0].size();
DFS(sr, sc, image, row, col, image[sr][sc], newColor);
return image;
}
private:
int a[4] = {0, 1, 0, -1};
int b[4] = {1, 0, -1, 0};
void BFS(int i, int j, vector<vector<int>>& image, int row, int col, int oldcolor, int newcolor)
{
queue<pair<int, int>> que;
que.push(make_pair(i, j));
image[i][j] = newcolor;
while (!que.empty())
{
int x = que.front().first, y = que.front().second;
que.pop();
for (int k = 0; k < 4; k++)
{
int _x = x + a[k], _y = y + b[k];
if ( isvaild(_x, _y, row, col) && image[_x][_y] == oldcolor)
{
que.push(make_pair(_x, _y));
image[_x][_y] = newcolor;
}
}
}
}
void DFS(int i, int j, vector<vector<int>>& image, int row, int col, int oldcolor, int newcolor)
{
stack<pair<int, int> >stk;
stk.push(make_pair(i, j));
image[i][j] = newcolor;
while (!stk.empty())
{
bool hasfindone = false; //DFS不用函数嵌套的写法就必须要一个记录当前点周围必须没有新的点加入,才能把当前点pop,不然会找不到当前点的其他邻接点。
int x = stk.top().first, y = stk.top().second;
for (int k = 0; k < 4; k++)
{
int _x = x + a[k], _y = y + b[k];
if (isvaild(_x, _y, row, col) && image[_x][_y] == oldcolor)
{
hasfindone = true;
image[_x][_y] = newcolor;
stk.push(make_pair(_x, _y));
break;
}
}
if (!hasfindone) //这个点周围都没有了。
{
stk.pop();
}
}
}
bool isvaild(int x, int y, int row, int col)
{
return x >= 0 && x < row && y >= 0 && y < col;
}
};