lc 733 Flood Fill


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

DFS Accepted

这道题是非常简单的dfs算法题,对于当前点,如果其原始值等于image[sr][sc]的原始值,那么将其值替换为newColor,并向其四个方向继续做dfs,直到遇到边界或者下一个原始值不等于image[sr][sc]的原始值。注意:如果image[sr][sc]的原始值就等于newColor的话,那直接返回image,不用做洪泛。

class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color != newColor) dfs(image, color, newColor, sr, sc);
return image;
}
void dfs(vector<vector<int>>& image, int color, int n, int sr, int sc) {
if (image[sr][sc] == color) {
image[sr][sc] = n;
if (sr - 1 >= 0) dfs(image, color, n, sr-1, sc);
if (sc - 1 >= 0) dfs(image, color, n, sr, sc-1);
if (sr + 1 < image.size()) dfs(image, color, n, sr+1, sc);
if (sc + 1 < image[0].size()) dfs(image, color, n, sr, sc+1);
}
}
};

LN : leetcode 733 Flood Fill的更多相关文章

  1. 【Leetcode_easy】733. Flood Fill

    problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...

  2. 【LeetCode】733. Flood Fill 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

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

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

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

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

  6. 733. Flood Fill

    class Solution { public: int szx,szy; vector<vector<int>> floodFill(vector<vector< ...

  7. Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)

    Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...

  8. [LeetCode] Flood Fill 洪水填充

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

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

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

随机推荐

  1. iOS webView的常见属性和方法

    一.初始化与三种加载方式 UIWebView继承与UIView,因此,其初始化方法和一般的view一样,通过alloc和init进行初始化,其加载数据的方式有三种: 第一种: - (void)load ...

  2. Android 源码架构

    我们都知道Android系统是一个开源工程,在网上可以下载到源代码. 一般在网上搜索一下,就会找到各种下载源代码的方法,比如使用Git和Repo,android源代码下载的网址是http://andr ...

  3. (深入理解计算机系统) bss段,data段、text段、堆(heap)和栈(stack)

    bss段: bss段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域. bss是英文Block Started by Symbol的简称. bss段属于静态内存分配. ...

  4. 北斗有 35 颗卫星,而 GPS 有 24 颗卫星,为什么二者数量不同?

    作者:知乎用户链接:https://www.zhihu.com/question/21092045/answer/17164418来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  5. POJ1300 Door Man —— 欧拉回路(无向图)

    题目链接:http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  6. POJ3660 Cow Contest —— Floyd 传递闭包

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  7. hdu 1286 找新朋友(欧拉函数)

    题意:欧拉函数 思路:欧拉函数 模板,代码略.

  8. java内存管理--栈、堆和常量池

    今天有朋友问java中String[] str = s.split(",")的内存分析,于是开始查资料并测试.首先,发现在java的内存管理中"常量池"是个很奇 ...

  9. [Java] public, private, protected

      同包不同类的成员 不同包中子类的成员 不同包非子类的成员 public √ √ √ protected √ √ × 默认 √ × × private × × ×

  10. hibernate的基础学习--一对多关联

    基本的用户和部门类,只有uuid和名称,没有其余字段. 配置文件 部门: <?xml version="1.0" encoding="utf-8" ?&g ...