733. Flood Fill
An image is represented by a 2-D array of integers, each integerrepresenting the pixel value of the image (from 0 to 65535).
Givena coordinate (sr, sc) representingthe starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
Toperform a "flood fill", consider the starting pixel, plus any pixelsconnected 4-directionally to the starting pixel of the same color as thestarting pixel, plus any pixels connected 4-directionally to those pixels (alsowith the same color as the starting pixel), and so on. Replace the color of allof the aforementioned pixels with the newColor.
Atthe end, return the modified image.
Example1:
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 coloredwith the new color.
Note the bottom corner is not colored 2, because it is not4-directionally connected
to the starting pixel.
Note:
· The length of image and image[0] willbe 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 willbe an integer in [0, 65535].
class Solution {
public:
#include
vector
queue< vector
vector
bool vst[51][51] = { false };
int num[3];
num[0] = sr; num[1] = sc;
theColorQ.push(vector
while (!theColorQ.empty())
{
p = theColorQ.front();theColorQ.pop();
int x, y;
x = p[0]; y = p[1];
vst[x][y] = true;
if (x>0 && (!vst[x -1][y]) && image[x - 1][y] == image[x][y]) { num[0] = x - 1; num[1] = y;theColorQ.push(vector
if (x
if (y>0 && (!vst[x][y -1]) && image[x][y - 1] == image[x][y]) { num[0] = x; num[1] = y - 1;theColorQ.push(vector
if(y
image[x][y] = newColor;
}
return image;
}
};