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的更多相关文章

  1. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  2. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  3. [LeetCode] Flood Fill 洪水填充

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

  4. [Swift]LeetCode733. 图像渲染 | Flood Fill

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

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

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

  6. [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 ...

  7. LeetCode - Flood Fill

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

  8. CF 1114 D. Flood Fill

    D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...

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

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

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

随机推荐

  1. Nowcoder的JS计时器题分析

    题目描述 实现一个打点计时器,要求1.从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 12.返回的对象中需要包含一个 ...

  2. webpack4 单入口文件配置 多入口文件配置 以及常用的配置

    单入口文件配置 webpack.config.js const path = require('path'); const HtmlWebpackPlugin = require('html-webp ...

  3. 这一次,彻底弄懂「Java字节码文件」

    提前祝福各位读者

  4. MySQL的安装教程

    一.MYSQL的安装 首先登入官网下载mysql的安装包,官网地址:https://dev.mysql.com/downloads/mysql/ 一般下载这个就好,现在的最新版本是5.8,但是据说已经 ...

  5. 安装PyTorch-Geometric包

    pip install torch-scatter # 报错 error: Microsoft Visual C++ 14.0 is required. Get it with "Micro ...

  6. C++入门经典-例8.3-子类显示调用父类构造函数

    1:当父类含有带参数的构造函数时,创建子类的时候会调用它吗?答案是通过显示方式才可以调用. 无论创建子类对象时调用的是那种子类构造函数,都会自动调用父类默认构造函数.若想使用父类带参数的构造函数,则需 ...

  7. nvl(sum(字段),0) 的时候,能展示数据0,但是group by 下某个伪列的时候,查不到数据(转载)

    今天碰到一个比较有疑惑的问题,就是在统计和的时候,我们往往有时候查不到数据,都会再加个 nvl(sum(字段),0) 来显示这个字段,但是如果我们再加个group by ,就算有加入这个 nvl(nu ...

  8. java html table 转 excel,给予jdom 和 poi

    maven 引入 <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artif ...

  9. C++中的to_string()

    目录 C++中的to_string() 注:原创不易,转载请务必注明原作者和出处,感谢支持! C++中的to_string() C++中的 to_string()系列函数将数值转换成字符串形式.注意, ...

  10. Dao操作的抽取

    package com.loaderman.demo.c_jdbc; public class Admin { private int id; private String userName; pri ...