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.
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if (image[sr][sc] == newColor) return image;
fill(image, sr, sc, image[sr][sc], newColor);
return image;
}
private void fill(int[][] image, int sr, int sc, int color, int newColor) {
if (sr < || sr >= image.length || sc < || sc >= image[].length || image[sr][sc] != color) return;
image[sr][sc] = newColor;
fill(image, sr + , sc, color, newColor);
fill(image, sr - , sc, color, newColor);
fill(image, sr, sc + , color, newColor);
fill(image, sr, sc - , color, newColor);
}
}
Flood Fill的更多相关文章
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- [Swift]LeetCode733. 图像渲染 | 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 ...
- [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 - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- CF 1114 D. Flood Fill
D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...
- Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】
任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory ...
- 733. Flood Fill 简单型染色问题
[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value ...
随机推荐
- 小程序 swiper 轮播图滚动图片 + 视频
直奔代码主题wxml: <view class="test_box"> <swiper indicator-dots="{{indicatorDots} ...
- python测试网站访问速度
# -*- coding: utf-8 -*- # @Author : Felix Wang # @time : 2018/8/13 22:13 # pip3 install pycurl impor ...
- Python与开源GIS
https://www.osgeo.cn/pygis/ 这里列出了与 GIS 相关的 Python 开源类库与工具. 基础类库(抽象库) • GDAL/OGR 是大部分开源GIS的基础,也包括如Arc ...
- Spring——代理实现AOP
一.静态代理实现 1.接口(抽象主题) 2.接口的实现类(真实主题) 3.代理类(代理主题) 4.测试类: ApplicationContext context=new ClassPathXmlApp ...
- Java web分级测试评分C级感受
上周一进行了java分级测试,但是完成的不太好,先看题目: 石家庄铁道大学选课管理系统 1.项目需求: 本项目所开发的学生选课系统完成学校对学生的选课信息的统计与管理,减少数据漏掉的情况,同时也节约人 ...
- Kamil and Making a Stream
E. Kamil and Making a Stream 参考:Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上 ...
- Centos7下设置MySql自动启动
原文链接:http://www.cnblogs.com/Sungeek/p/9687565.html 1.将服务文件拷贝到init.d下,并重命名为mysql cp /usr/local/mysql/ ...
- $\LaTeX$数学公式大全8
$8\ Miscellaneous\ symbols$ $\infty$ \infty $\nabla$ \nabla $\partial$ \partial $\eth$ \eth $\clubsu ...
- finally的一个妙用
●传统用法try-catch-finally大家都会用:try包裹可能抛出异常的代码:catch捕获异常并对其处理:finally做一些资源关闭等回收工作.简单明了一句话就能说清. 稍微进阶一些的,大 ...
- 3.开始使用Spring Cloud实战微服务
开始使用Spring Cloud实战微服务 3.1. Spring Cloud实战前提 3.1.1. 需要的技术储备 语言方面:可以使用Java.scala.Groo ...