leetcode原题:颜色填充(经典的递归问题)

题目:

编写函数,实现许多图片编辑软件都支持的「颜色填充」功能。

待填充的图像用二维数组 image 表示,元素为初始颜色值。初始坐标点的行坐标为 sr 列坐标为 sc。需要填充的新颜色为 newColor 。

「周围区域」是指颜色相同且在上、下、左、右四个方向上存在相连情况的若干元素。

请用新颜色填充初始坐标点的周围区域,并返回填充后的图像。

示例:

输入:

image =[[1,1,1],[1,1,0],[1,0,1]]sr = 1,sc = 1,newColor = 2

输出:

[[2,2,2],[2,2,],[2,0,1]]

解释:

初始坐标点位于图像的正中间,坐标 (sr,sc)=(1,1)。

初始坐标点周围区域上所有符合条件的像素点的颜色都被更改成 2。

注意,右下角的像素没有更改为 2,因为它不属于初始坐标点的周围区域。

解题思路:

1.首先要保存初始颜色值,以便后续判断是否是连续的

2.定义四个方向的偏移量,上下左右

3.分别找四个方向的坐标,将符合条件的进行颜色更新

源代码如下:

cpp 复制代码
class Solution {
public:
    const int dx[4] = {1, 0, 0, -1};//分别是上下左右四个方向
    const int dy[4] = {0, 1, -1, 0};
    void dfs(vector<vector<int>>& image, int x, int y, int color, int newColor) {
        //如果当前坐标的颜色是初始颜色
        if (image[x][y] == color) {
            //更新当前坐标的颜色
            image[x][y] = newColor;
            //递归地找上下左右四个方向
            for (int i = 0; i < 4; i++) {
                int mx = x + dx[i], my = y + dy[i];
                //新坐标在图像的范围内,才进行递归,否则继续找其他方向的
                if (mx >= 0 && mx < image.size() && my >= 0 && my < image[0].size()) {
                    dfs(image, mx, my, color, newColor);
                }
            }
        }
    }
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        //记录初始颜色值
        int currColor = image[sr][sc];
        //需要更新的颜色不是初始颜色,开始递归
        if (currColor != newColor) {
            dfs(image, sr, sc, currColor, newColor);
        }
        //返回原数组
        return image;
    }
};

这道题还可以用广度优先遍历来实现,借助队列,在队列中通过pair对保存坐标的x,y。

将四个方向中符合条件的坐标入队,再进行更新颜色

源代码如下:

cpp 复制代码
class Solution {
public:
    const int dx[4]={1,0,0,-1};
    const int dy[4]={0,1,-1,0};
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int currColor = image[sr][sc];//保存初始颜色值
        //如果要更新的颜色值与初始值相同,就不用改了,直接返回数组
        if (currColor == newColor) {
            return image;
		}
        //记录图像的行和列
        int n = image.size(), m = image[0].size();
        //在队列中保存的是一个个pair对
        queue<pair<int, int>> que;
        //先将初始值入队
        que.emplace(sr, sc);
        //初始坐标更新颜色
        image[sr][sc] = newColor;
        while (!que.empty()) {
            //取坐标
            int x = que.front().first, y = que.front().second;
            //坐标出队
            que.pop();
            //查找四个方向的坐标是否相连
            for (int i = 0; i < 4; i++) {
                int mx = x + dx[i], my = y + dy[i];
                //坐标需要在图像范围内且与初始坐标相连,也就是与初始颜色相同
                if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) {
                    //坐标入队
                    que.emplace(mx, my);
                    //更新颜色
                    image[mx][my] = newColor;
                }
            }
        }
        return image;
    }
};
相关推荐
程序趣谈9 分钟前
算法随笔_36: 复写零
数据结构·python·算法
CHANG_THE_WORLD9 分钟前
C++并发编程指南04
开发语言·c++
轩情吖21 分钟前
二叉树-堆(补充)
c语言·数据结构·c++·后端·二叉树··排序
九亿AI算法优化工作室&31 分钟前
GWO优化LSBooST回归预测matlab
人工智能·python·算法·机器学习·matlab·数据挖掘·回归
爱是小小的癌2 小时前
Java-数据结构-优先级队列(堆)
java·前端·数据结构
sjsjs112 小时前
【数据结构-字典树】力扣14. 最长公共前缀
数据结构·leetcode
python算法(魔法师版)2 小时前
基于机器学习鉴别中药材的方法
深度学习·线性代数·算法·机器学习·支持向量机·数据挖掘·动态规划
JNU freshman3 小时前
力扣第435场周赛讲解
算法·leetcode·蓝桥杯
眼镜哥(with glasses)3 小时前
蓝桥杯python基础算法(2-2)——基础算法(B)——模拟(上)
算法
赵鑫亿5 小时前
7.DP算法
算法·dp