zoukankan      html  css  js  c++  java
  • 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.
    

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    忘了dfs的模板格式了,以为退出条件要分开写。但其实退出条件是提前写到一起的。

    [一句话思路]:

    先染头一个点,形成基础,再四周扩展(还是0 to n-1) 染别的点。不是空中楼阁

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 本题中,原色不想等 不是color的点不能染 。退出条件中,范围在前,特殊情况在后。

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    退出条件中,范围在前,特殊情况在后。

    [复杂度]:Time complexity: O() Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    退出条件 范围在前

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
            //cc
            if (image[sr][sc] == newColor) return image;
            
            //dfs
            dfs(image, sr, sc, image[sr][sc], newColor);
            
            //return
            return image;
        }
        
        public void dfs(int[][] image, int sr, int sc, int color, int newColor) {
            //exit;
            if (sr < 0 || sr >= image.length || sc < 0 || sc>= image[0].length || color != image[sr][sc]) return ;
            
            //first color
            image[sr][sc] = newColor;
            
            //expand
            dfs(image, sr + 1, sc, color, newColor);
            dfs(image, sr - 1, sc, color, newColor);
            dfs(image, sr, sc + 1, color, newColor);
            dfs(image, sr, sc - 1, color, newColor);
        }
    }
    View Code
  • 相关阅读:
    CF1175B Catch Overflow!
    震惊!一蒟蒻竟然写出fhqTreap
    树上差分
    洛谷 P3128 最大流Max Flow
    线段树的标记永久化/二维线段树模板
    矩阵加速~desire drive
    置换相关
    树形图们
    严格单调递增与非严格之间的转换
    记录延续性的一类dp
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8971361.html
Copyright © 2011-2022 走看看