题目描述:

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. PEAR

    简介:pear是php扩展与应用库(the php extension and application repository)的缩写.它是一个php扩展及应用的一个代码仓库. 编码规范:参考(http ...

  2. OLI 课程 & Java入学考试的五道题

    Unit  1:: Programming with Java ✔️ 机械.自动.不需要智慧地执行原始的内置指令. 字节码相同,JVM不同(体现平台) ✖️ In modern computers i ...

  3. [leetcode]253. Meeting Rooms II 会议室II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  4. Homestead window10 storage:link 不能建立符号链接的处理办法

    重启电脑 1. 以管理员身份运行 cmd 2. vagrant up 3. vagrant ssh 4. php artisan storage:link

  5. KbmMW 4.40.00 测试发布

    经过漫长的等待,支持移动开发的kbmmw 4.40.00 终于发布了,这次不但支持各个平台的开发, 而且增加了认证管理器等很多新特性,非常值得升级.具体见下表. 4.40.00 BETA 1 Oct ...

  6. phonegap/cordova学习建议

    在技术群里面,一直有一些新人进来,问了一些让人可笑不得的问题.国内的资料相对比较少,而且很旧,都是一些2.X版本的资料.因此想写一些东西,帮助一下新人,让他们少走弯路. 首先说一些很多人问的问题,个人 ...

  7. 层层递进Struts1(五)之处理流程

    这篇博客我们深入Struts框架执行部分源码,从ActionServlet的process函数开始,看一下其内在的执行过程. 流程图 以下流程图展示的是ActionServlet和RequestPro ...

  8. 我的第一个博客&GuiHub简单练习

    个人介绍 姓名:马瑞 性别:男 班级:网络工程143 出生年月:1995.11 兴趣爱好:玩玩电脑,看看动漫. 编程能力:完全是菜鸟.   GutHub的使用体验:  第一步:注册github,这很简 ...

  9. urlrewrite重写url(转)

    环境: Maven 3.0.4 Urlrewrite 2.5.2 Myeclipse 8.6.1 借此机会顺便提一下 Maven Project 的创建,会了的朋友或还不想了解 Maven 的朋友,可 ...

  10. 【转】不用软件,解压Win8/Win8.1的install.wim文件

    今天用好压解压Windows 8.1的install.wim文件,居然提示文件损坏,换了7Z仍然如此:其实文件是好的.只不过这些软件暂时不支持罢了,还好可以用dism命令来手动完成. 一.检查镜像版本 ...