题目描述:

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.

To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.

At the end, return the modified image.

Example 1:

Input:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.

Note:

  • The length of image and image[0] will be in the range [1, 50].
  • The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
  • The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

要完成的函数:

vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor)

说明:

1、这道题给定一个二维vector表示一张图片,一个sr表示起始点的行坐标,sc表示起始点的纵坐标,要求从起始点开始,找到四个方向上跟起始点相同颜色的像素点,把起始点以及周边找到的点都给改变颜色,变成给定的newcolor。之后再从周边找到的点往外扩散,相同方法找到新的点,继续改变颜色……

2、这是一道DFS或者BFS的题目,笔者对于BFS比较熟悉,也就写成了BFS。

代码如下:(附详解)

    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor)
{
int oldcolor=image[sr][sc];
if(oldcolor==newColor)return image;//如果旧的颜色和新的颜色一样,那么根本不需要改变,返回原本的image就好了
queue<int>q1;//定义一个队列
q1.push(sr);//塞入行坐标
q1.push(sc);//塞入纵坐标
while(!q1.empty())//当队列非空的情况下,迭代处理
{
sr=q1.front();//取出行坐标
q1.pop();
sc=q1.front();//取出列在坐标
q1.pop();
image[sr][sc]=newColor;//改变当前点的颜色,同时避免之后的重复寻找
if(sr-1>=0&&image[sr-1][sc]==oldcolor)//判断上方点是不是相同颜色
{
q1.push(sr-1);
q1.push(sc);
}
if(sr+1<image.size()&&image[sr+1][sc]==oldcolor)//判断下方点是不是相同颜色
{
q1.push(sr+1);
q1.push(sc);
}
if(sc-1>=0&&image[sr][sc-1]==oldcolor)//判断左边的点是不是相同颜色
{
q1.push(sr);
q1.push(sc-1);
}
if(sc+1<image[0].size()&&image[sr][sc+1]==oldcolor)//判断右边的点是不是相同颜色
{
q1.push(sr);
q1.push(sc+1);
}
}
return image;
}

上述代码实测35ms,beats 68.76% of cpp submissions。

leetcode-733-Flood Fill的更多相关文章

  1. LN : leetcode 733 Flood Fill

    lc 733 Flood Fill 733 Flood Fill An image is represented by a 2-D array of integers, each integer re ...

  2. 【Leetcode_easy】733. Flood Fill

    problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...

  3. 【LeetCode】733. Flood Fill 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  4. [LeetCode&Python] Problem 733. Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  5. [LeetCode] 733. Flood Fill_Easy tag: BFS

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  6. 733. Flood Fill 简单型染色问题

    [抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value ...

  7. 733. Flood Fill

    class Solution { public: int szx,szy; vector<vector<int>> floodFill(vector<vector< ...

  8. Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)

    Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...

  9. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  10. LeetCode刷题 Flood Fill 洪水填充问题

    An  image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...

随机推荐

  1. 265. Paint House II 房子涂色K种选择的版本

    [抄题]: There are a row of n houses, each house can be painted with one of the k colors. The cost of p ...

  2. 286被围绕的区域 · Surrounded Regions

    [抄题]: 给一个二维的矩阵,包含 'X' 和 'O', 找到所有被 'X' 围绕的区域,并用 'X' 填充满. 样例 给出二维矩阵: X X X X X O O X X X O X X O X X ...

  3. SQLSERVER CROSS APPLY 与 OUTER APPLY 的应用

    日常开发中遇到多表查询时,首先会想到 INNER JOIN 或 LEFT OUTER JOIN 等等,但是这两种查询有时候不能满足需求.比如,左表一条关联右表多条记录时,我需要控制右表的某一条或多条记 ...

  4. 网络中的A、B、C类地址

    1.A类ip地址(1.0.0.0到126.255.255.255) A类地址只有第一个8位表示网络地址,最高位一定为0,所以A类地址的网络号范围可以为:64+32+16+8+4+2+1=127,也就是 ...

  5. deploy: [mkdir] Created dir: C:\Program Files\Java\apache-cxf-2.4.2\samples\java_first_pojo\build [loadfile] Do not set property srcbuild.classpath as its length is 0.

    使用CXF的错误,错误是说我的路径有错误,因为路径错误所以无法运行程序 (1)原因,我将其放入了Program Files文件夹下,所以,其不好使 分析原因: 目录路径错误,目录中不能有空格,否则其解 ...

  6. eclipse 在线安装 properties 插件

    help - install new software - work with site http://propedit.sourceforge.jp/eclipse/updates/ 要耐心等待

  7. swift 学习之UISegmentedControl

    //创建分段控件的标题         let titileArray:[String] = ["点评", "哪里逃"]         let segment ...

  8. Android代码实现求和运算

    Android代码实现求和运算 实验要求: 用Android语言设计一个界面,点击某按钮实现求和运算. 代码实现 码云链接 核心代码 以上为求和按钮的代码截图,解析如图标注. 实验结果 当输入为空值时 ...

  9. Linux 基础教程 33-硬盘分区及挂载

    挂载命令     在Windows系统中如果插入了U盘.移动硬盘.光驱等,只要能被Windows系统识别出来,则系统会进行自动挂载并添加盘符,然后我们就可以访问,而这一切均由系统完成,用户并不需要做任 ...

  10. [label][Smarty]Smarty使用心得

    Smarty模板引擎,使用smarty好处就是可以实现页面缓存,从而加快了初始化之后的页面访问速度. 某种程度上,smarty模板确保了template页面的代码整洁,避免了HTML标记与PHP的混合 ...