733. 图像渲染
来自 <https://leetcode.cn/problems/flood-fill/>
class Solution {
public:
// bfs
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
int n=image.size();
int m=image[0].size();
// vector<vector<int>> (n,vector<int>(m,0)) vist;
int vist[100][100]={0};
queue<pair<int,int>> q;
q.push({sr,sc});
vist[sr][sc]=1;
while(q.size()){
auto t= q.front();
q.pop();
for(int i=0;i<4;i++){
int x=t.first+dx[i];
int y=t.second+dy[i];
if(x>=0 && x<n && y>=0 && y<m && vist[x][y]==0 && image[x][y]==image[sr][sc]){
image[x][y]=color;
vist[x][y]=1;
// 添加广度遍历到的结点
q.push({x,y});
}
}
}
image[sr][sc]=color;
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// cout<<image[i][j]<<" ";
// }
// cout<<endl;
// }
return image;
}
};
标签:vist,渲染,int,image,733,vector,图像,size
From: https://www.cnblogs.com/mengfengguang/p/16790170.html