leetcode-733-Flood Fill
题目描述:
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
andimage[0]
will be in the range[1, 50]
. - The given starting pixel will satisfy
0 <= sr < image.length
and0 <= sc < image[0].length
. - The value of each color in
image[i][j]
andnewColor
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的更多相关文章
- 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 ...
- 【Leetcode_easy】733. Flood Fill
problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...
- 【LeetCode】733. Flood Fill 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- [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 ...
- [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 ...
- 733. Flood Fill 简单型染色问题
[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value ...
- 733. Flood Fill
class Solution { public: int szx,szy; vector<vector<int>> floodFill(vector<vector< ...
- Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)
Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- LeetCode刷题 Flood Fill 洪水填充问题
An image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...
随机推荐
- code1135 选择客栈
首先,预处理三个数组. pre[x]表示在此之前颜色为x的客栈有多少个. f[x]表示在此之前的客栈中,某个点c,c的颜色为x,并且从c点到已经读入的点之间有费用小于p的客栈,这样的c点的个数 las ...
- CRC32加密算法原理
[转]:https://blog.csdn.net/android_mnbvcxz/article/details/78902737
- nginx 负载均衡 使用ip_hash方式解决session问题 测试
ip_hash的方式比较弱智,但是在一般情况下是挺有效的~~,如果能保证nginx是最上一层的代理,那么能够得到用户的ip是真实位置,就能做到负载,但是一家公司的所有员工其实走的是同一个ip,那么在这 ...
- DB2日期转格式化字符串
DB2 应该有个 TO_CHAR 的函数. 用来把 日期 转换为 字符串 1 2 3 4 5 6 7 8 9 10 db2 => SELECT db2 (cont.) => TO ...
- console.dir(someObject);
<script type="text/javascript"> function test(){ var array = [{"id":1},{&q ...
- Monocular Visual-Inertial Odometry单目视觉惯性里程计
Monocular Visual-Inertial Odometry:https://www.qualcomm.com/invention/research 单目视觉-惯性里程计 INDOOR POS ...
- 23 DesignPatterns学习笔记:C++语言实现 --- 1.4 Builder
23 DesignPatterns学习笔记:C++语言实现 --- 1.4 Builder 2016-07-21 (www.cnblogs.com/icmzn) 模式理解
- delphi Overload 和override的区别
overload是重载;相同的函数名,参数不同,使用不同的函数体 override 是对父类声明的vitural或dynamic方法进行覆盖 overload的使用方法: [delphi] v ...
- NETSH.EXE操作SSL
NETSH.EXE操作SSL 程序位置:c:\windows\syswow64\netsh.exe 查看当前端口配置 netsh http show sslcert 将 SSL 证书绑定至端口号 ne ...
- 基于python 3.5 所做的找出来一个字符串中最长不重复子串算法
功能:找出来一个字符串中最长不重复子串 def find_longest_no_repeat_substr(one_str): #定义一个列表用于存储非重复字符子串 res_list=[] #获得字符 ...