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

从给定坐标开始,将与该点相邻  并且值也相同  的点的 值都换成newColor

C++(31ms):  DFS

 1 class Solution {
 2 public:
 3     vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
 4         if (image[sr][sc] != newColor){
 5             dfs(image , sr , sc , image[sr][sc] , newColor) ;
 6         }
 7         return image ;
 8     }
 9     void dfs(vector<vector<int>>& image, int i, int j, int oldColor ,int newColor){
10         if (i < 0 || j < 0 || i >= image.size() || j >= image[0].size() || image[i][j] != oldColor)
11             return ;
12         image[i][j] = newColor ;
13         dfs(image,i-1 , j , oldColor , newColor) ;
14         dfs(image,i+1 , j , oldColor , newColor) ;
15         dfs(image,i , j-1 , oldColor , newColor) ;
16         dfs(image,i , j+1 , oldColor , newColor) ;
17     }
18 };

原文地址:https://www.cnblogs.com/mengchunchen/p/8541546.html

时间: 2024-11-08 22:39:24

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 newC

733. Flood Fill - Easy

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)算法

从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色,直到封闭区域内的所有节点都被处理过为止,是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法. 因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名.在GNU Go和扫雷中,Flood Fill算法被用来计算需要被清除的区域. 洪水填充算法接受三个参数:起始节点,目标节点特征和针对提取对象要执行的处理. 目前有许多实现方式,基本上都显式的或隐式的使用了队列或者栈. 洪水填充算法实现最常见有四邻域

USACO The Castle(flood fill)

题目请点我 题解: 这道题真的好蛋疼啊,首先题意不好理解,搞了半天复杂的要死,有那么多要求,还要求那么多东西,做到一半都不想做了...感觉没什么技术含量,还做起来死费劲儿.但是强迫症非得按顺序做题啊,最后还是一点点把它给调出来了,说什么flood fill,其实也就是那么回事,没什么算法上的技巧,就是见招拆招的感觉... 题意搞懂再做题,题意,不谢! 第一步:根据他的规则把房间画出来,遍历一遍把每个节点四面的墙给补上: 第二步:深搜: 目的1:记录深搜的次数,房间数: 目的2:记录深搜的深度,最

SSOJ 2316 面积【DFS/Flood Fill】

题目描述 编程计算由“1”号围成的下列图形的面积.面积计算方法是统计1号所围成的闭合曲线中点的数目. 如图所示,在10*10的二维数组中,“1”围住了15个点,因此面积为15. 题目大意:对于给定的10*10的01矩阵,请问有多少个0被1包围了?(包围是指不能由上下左右通向边缘)本文来源于OIER博客,原文出处:http://www.oier.cc/ssoj2316%E9%9D%A2%E7%A7%AF/ 解题思路 图形学中Flood Fill是满水法填充,是用来填充区域的.就好比在一个地方一直到

[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,

CF 1114 D. Flood Fill

D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 分析: 区间dp. dp[i][j][0/1]表示当前的区间是l~r,把这一段变成一个颜色的最少次数,最后一个改变的颜色是最左边/最右边的一段. 代码: #include<cstdio> #include<algorithm> #include<cstring> #inc

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 limit per test 256 megabytes input standard input output standard output You are given a line of nn colored squares in a row, numbered from 11 to nn f

CodeForces - 1114D Flood Fill (区间dp)

You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ii-th square initially has the color cici. Let's say, that two squares ii and jj belong to the same connected component if ci=cjci=cj, and ci=ckci=ck