力扣洪水算法:图像渲染

129 篇文章 1 订阅
96 篇文章 0 订阅
本文介绍了如何在LeetCode平台上使用深度优先搜索(DFS)算法实现图像渲染中的FloodFill功能,通过递归遍历矩阵并更新颜色,以找出给定颜色的区域。
摘要由CSDN通过智能技术生成

1.733. 图像渲染 - 力扣(LeetCode)

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;
    }
};

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值