[LeetCode] 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
imageandimage[0]will be in the range[1, 50]. - The given starting pixel will satisfy
0 <= sr < image.lengthand0 <= sc < image[0].length. - The value of each color in
image[i][j]andnewColorwill be an integer in[0, 65535].
这道题给了一个用二维数组表示的图像,不同的数字代表不同的颜色,给了一个起始点坐标,还有一个新的颜色,让我们把起始点的颜色以及其相邻的同样的颜色都换成新的颜色。实际上就是一个找相同区间的题,可以用 BFS 或者 DFS 来做。先来看 BFS 的解法,使用一个队列 queue 来辅助,首先将给定点放入队列中,然后进行 while 循环,条件是 queue 不为空,然后进行类似层序遍历的方法,取出队首元素,将其赋值为新的颜色,然后遍历周围四个点,如果不越界,且周围的颜色跟起始颜色相同的话,将位置加入队列中,参见代码如下:
解法一:
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int m = image.size(), n = image[].size(), color = image[sr][sc];
vector<vector<int>> res = image;
vector<vector<int>> dirs{{,-},{-,},{,},{,}};
queue<pair<int, int>> q{{{sr, sc}}};
while (!q.empty()) {
int len = q.size();
for (int i = ; i < len; ++i) {
auto t = q.front(); q.pop();
res[t.first][t.second] = newColor;
for (auto dir : dirs) {
int x = t.first + dir[], y = t.second + dir[];
if (x < || x >= m || y < || y >= n || res[x][y] != color) continue;
q.push({x, y});
}
}
}
return res;
}
};
DFS 的写法相对简洁一些,首先判断如果给定位置的颜色跟新的颜色相同的话,直接返回,否则就对给定位置调用递归函数。在递归函数中,如果越界或者当前颜色跟起始颜色不同,直接返回。否则就给当前位置赋上新的颜色,然后对周围四个点继续调用递归函数,参见代码如下:
解法二:
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if (image[sr][sc] == newColor) return image;
helper(image, sr, sc, image[sr][sc], newColor);
return image;
}
void helper(vector<vector<int>>& image, int i, int j, int color, int newColor) {
int m = image.size(), n = image[].size();
if (i < || i >= m || j < || j >= n || image[i][j] != color) return;
image[i][j] = newColor;
helper(image, i + , j, color, newColor);
helper(image, i, j + , color, newColor);
helper(image, i - , j, color, newColor);
helper(image, i, j - , color, newColor);
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/733
类似题目:
参考资料:
https://leetcode.com/problems/flood-fill/
https://leetcode.com/problems/flood-fill/discuss/109584/java-9-liner-dfs
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Flood Fill 洪水填充的更多相关文章
- LeetCode刷题 Flood Fill 洪水填充问题
An image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...
- LeetCode - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 【LeetCode】733. Flood Fill 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)
Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...
- [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 ...
- 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 ...
- [Swift]LeetCode733. 图像渲染 | Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
随机推荐
- 本地不安装oracle,PLsql远程连接
Oracle的Instant client工具包可以很好地解决本地不安装oracle,PLsql远程连接. 1.首先到Oracle网站下载Instant Client : http://www.ora ...
- 修改GeoJson的网址
http://geojson.io 可以打开自己的json 然后修改
- 开源小工具 酷狗、网易音乐缓存文件转mp3工具
发布一个开源小工具,支持将酷狗和网易云音乐的缓存文件转码为MP3文件. 以前写过kgtemp文件转mp3工具,正好当前又有网易云音乐缓存文件需求,因此就在原来小工具的基础上做了一点修改,增加了对网易云 ...
- 一周Maven框架学习随笔
第一次写博客,可能写得不是很好,但是希望自己持之以恒,以后会更好.也希望通过写博客记录随笔,让自己本身有所收获. 下面是今天的maven总结: maven个人理解中是Maven项目对象模型(POM), ...
- 多目标跟踪(MOT)论文随笔-SIMPLE ONLINE AND REALTIME TRACKING WITH A DEEP ASSOCIATION METRIC (Deep SORT)
网上已有很多关于MOT的文章,此系列仅为个人阅读随笔,便于初学者的共同成长.若希望详细了解,建议阅读原文. 本文是tracking by detection 方法进行多目标跟踪的文章,在SORT的基础 ...
- 福州大学W班-Beta冲刺评分
作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1428 作业要求 1.博客具体要求 昨天的困 ...
- C语言的第一次博客作业
题目1:7-3 温度转换 1.代码 int fahr, celsius; fahr=150; celsius=5*(fahr-32)/9; printf("fahr = %d, celsiu ...
- 201621123044《JAVA程序设计》第一周学习总结
1. 本周学习总结 1.了解了JAVA的诞生以及发展历史简介.JAVA语言的特点,以及JAVA的电脑安装以及环境配置. 2.JAVA不仅可以用eclipse进行编写,也可以在记事本和notepad++ ...
- iOS开发-即时通信XMPP
1. 即时通信 1> 概述 即时通讯(Instant Messaging)是目前Internet上最为流行的通讯方式,各种各样的即时通讯软件也层出不穷,服务提供商也提供了越来越丰富的通讯服务功能 ...
- 怎么用DreamWare新建立一个静态网站的站点
可以上面的图可以看出首先需要用F8确定这个文件是勾选的,然后在D盘建立"华为"文件夹,然后在里面建js,css,image文件夹,然后在DW里面点击站点 然后点击管理站点,有一个新 ...