LN : leetcode 733 Flood Fill
lc 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
imageandimage[0]will be in the range[1, 50].The given starting pixel will satisfy
0 <= sr < image.lengthand0 <= sc < image[0].length.The value of each color in
image[i][j]andnewColorwill 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的更多相关文章
- 【Leetcode_easy】733. Flood Fill
problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...
- 【LeetCode】733. Flood Fill 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- [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] 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 ...
- 733. Flood Fill 简单型染色问题
[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value ...
- 733. Flood Fill
class Solution { public: int szx,szy; vector<vector<int>> floodFill(vector<vector< ...
- Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)
Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...
- [LeetCode] 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 ...
随机推荐
- ios 关于动画用法的总结
#import "FirstVC.h" @implementation FirstVC /* 创建xib过程 1 创建xib(名字和类名相同) 2 文件 ...
- jvm虚拟机配置 深度好文
http://blog.csdn.net/kthq/article/details/8618052
- 底层并发APIs_源自objc.io
本文由webfrogs译自objc.io,原文作者Daniel Eggert. 小引 本篇英文原文所发布的站点objc.io是一个专门为iOS和OS X开发者提供的深入讨论技术的平台,文章含金量很 ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- [Selenium] Automation Test Manual(Selenium)
http://www.cnblogs.com/puresoul/p/3483055.html http://www.360doc.com/content/14/0913/10/13497042_409 ...
- 依赖倒置原则DIP&控制反转IOC&依赖注入DI
依赖倒置原则DIP是软件设计里一个重要的设计思想,它规定上层不依赖下层而是共同依赖抽象接口,通常可以是上层提供接口,然后下层实现接口,上下层之间通过接口完全透明交互.这样的好处,上层不会因依赖的下层修 ...
- vue 组件 props 和event
组件是可扩展的HTML元素,封装可重用的代码. 使用祖册的组件,要确保在初初始化根实例之前注册组件 注册的组件中,data必须是函数 父组件通过props向子组件传递数据,子组件通过事件events给 ...
- Sublime text 安装Package Control
Package Control 插件是一个方便 Sublime text 管理插件的插件,但因为 Sublime Text 3 更新了 Python 的函数,API不同了,导致基于 Python 开发 ...
- 国外、国内各大OJ
下面是几个比较大的在线提交系统(Online Judge)里面有大量历年的竞赛题目,注册一个ID,然后用自己熟悉的语言(一般有Pascal/C/C++/Java)写好源代码提交即可,会实时返 回信息告 ...
- Oracle中插入100万条数据
在做项目的工程中,需要数据库中存在大量的数据进行程序的验证,但是我们又没有数据,这时就需要我们自己手动建一个表,插入大量数据,进行验证. 那么插入大量数据的sql语句如下: insert into E ...