Flood Fill

Flood Fill

Desc:

二维格点,用数字代表颜色,选定一个点(sr,sc)进行染色,和该点直接相连的同色点且都会被染成新颜色,求最后的二维矩阵。

很简单不知道为什么我要写一篇blog(表现的自己很努力?

My_Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int oldColor = image[sr][sc];
if (oldColor == newColor) return image;
queue<pair<int, int>> queue;
queue.push(make_pair(sr, sc));
int n = image.size();
int m = image[0].size();
while (!queue.empty()) {
int row = queue.front().first;
int column = queue.front().second;
queue.pop();
if (image[row][column] == oldColor) {
image[row][column] = newColor;
if (row != 0) queue.push(std::make_pair(row-1, column));
if (row != n-1) queue.push(std::make_pair(row+1, column));
if (column != 0) queue.push(std::make_pair(row, column-1));
if (column != m-1) queue.push(std::make_pair(row, column+1));
}
}
return image;
}
};