LeetCode 733. 图像渲染
原题链接
简单
作者:
Value
,
2020-08-16 18:50:32
,
所有人可见
,
阅读 347
class Solution {
public:
typedef pair<int, int> pii;
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int n = image.size();
int m = image[0].size();
vector<vector<bool>> flag(n, vector<bool>(m, false));
vector<vector<bool>> vis(n, vector<bool>(m, false));
queue<pii> qu;
qu.push({sr, sc});
vis[sr][sc] = true;
flag[sr][sc] = true;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int cmp = image[sr][sc];
while(!qu.empty()){
pii now = qu.front();
qu.pop();
for(int i = 0; i < 4; i ++ ){
int xx = now.first + dx[i];
int yy = now.second + dy[i];
if(xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
if(vis[xx][yy]) continue;
if(image[xx][yy] != cmp) continue;
qu.push({xx, yy});
vis[xx][yy] = true;
flag[xx][yy] = true;
}
}
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < m; j ++ ){
if(flag[i][j]) image[i][j] = newColor;
}
}
return image;
}
};