class Solution {
public:
bool check[100][100]={false};
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int m;
int n;
int p;
void dps(vector<vector<int>>&image,int i,int j,int color)
{
image[i][j]=color;
for(int k=0;k<4;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(x>=0&&x<m&&y>=0&&y<n&&image[x][y]==p)
{
dps(image,x,y,color);
}
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
if(image[sr][sc]==color)
{
return image;
}
m=image.size();
n=image[0].size();
p=image[sr][sc];
dps(image,sr,sc,color);
return image;
}
};
力扣洪水算法:图像渲染
最新推荐文章于 2024-10-06 16:32:56 发布
本文介绍了如何在LeetCode平台上使用深度优先搜索(DFS)算法实现图像渲染中的FloodFill功能,通过递归遍历矩阵并更新颜色,以找出给定颜色的区域。
摘要由CSDN通过智能技术生成