[CareerCup] 9.7 Paint Fill 填充
9.7 Implement the "paint fill" function that one might see on many image editing programs. That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color, fill in the surrounding area until the color changes from the original color.
这道题是一道填充问题,有点类似于Flash中的油桶工具,就是给指定的位置填充颜色,如果要填充的颜色和原来的颜色相同,则不发生变换,如果不同的话,则把相连接的区域都填充为新的颜色。那么我们使用递归来做,首先判断要填充的颜色和该位置上原有的颜色是否相同,如果不同的话就开始填充,如果周围颜色和起始位置颜色相同也填充,参见代码如下:
enum Color { Black, White, Red, Yellow, Green };
class Solution {
public:
bool paintFill(vector<vector<Color> > &screen, int x, int y, Color newColor) {
if (screen[x][y] == newColor) return false;
return paintFill(screen, x, y, screen[x][y], newColor);
}
bool paintFill(vector<vector<Color> > &screen, int x, int y, Color oldColor, Color newColor) {
if (x < || x >= screen.size() || y < || y >= screen[].size()) return false;
if (screen[x][y] == oldColor) {
screen[x][y] = newColor;
paintFill(screen, x - , y, oldColor, newColor);
paintFill(screen, x + , y, oldColor, newColor);
paintFill(screen, x, y - , oldColor, newColor);
paintFill(screen, x, y + , oldColor, newColor);
}
return true;
}
};
[CareerCup] 9.7 Paint Fill 填充的更多相关文章
- [Swift通天遁地]六、智能布局-(5)给视图添加Align(对齐)和Fill(填充的约束以及Label的约束
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- js--数组的 fill() 填充方法详解
前言 我们知道了很多了初始化数组的方法,但是初始化数组之后,数组中的每一项元素默认为 empty 空位占位,如何对数组这些空位添加默认的元素,ES6提供了 fill() 方法实现这一操作.本文总结数组 ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- HTML5 Canvas ( 填充图形的绘制 ) closePath, fillStyle, fill
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- fill 函数
fill函数的作用是:将一个区间的元素都赋予val值.函数参数:fill(first,last,val);//first为容器的首迭代器,last为容器的末迭代器,val为将要替换的值. 例题:给你n ...
- 绘图——Android绘图基础:Canvas、Paint等
Android的绘图应该继承View组件,并重写它的onDraw(Canvas canvas)方法即可. 重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Canv ...
- Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解
上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...
- Paint的基本使用
代码地址如下:http://www.demodashi.com/demo/14712.html 前言 在讲述自定义控件的时候,我们讲到了自定义控件的基本步骤,那么在自定义控件中,我们第一个需要了解的就 ...
随机推荐
- 有用的.NET库
1. Polly,重试 Polly is a .NET 3.5 / 4.0 / 4.5 / PCL library that allows developers to express transien ...
- Nuget~管理自己的包包~丢了的包包快速恢复
之前写过一篇Nuget~管理自己的包包的文章,今天来讲Nuget的另一个东西,就是找回丢失的DLL,我们在引用包包后,在当前解决方案根目录就生成一个packages的目前,里面有我们从nuget下载的 ...
- ajax跨域 自定义header问题总结
遇到的问题:已经在ajax里面添加自定义参数,但是没有生效 beforeSend : function(request) { request.setRequestHeader("region ...
- jquery实践案例--验证电子邮箱
<input type="email" name="email" id="email" value="" onpa ...
- 烂泥:通过vsphere给esxi添加本地硬盘
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 公司ESXi服务器的硬盘空间不够使用,现在新加了一块硬盘在ESxi服务器上.在服务器上添加完硬盘后,在Vsphere上是看不到新加硬盘的. 下面我们来通 ...
- 动态规划--模板--hdu 1059 Dividing
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- Golang tips ----- 函数
1.在函数调用时,Golang没有默认参数值 2.一个函数声明如果没有函数体,表面该函数不是由Golang实现的,这样的声明定义了函数标识符 3.拥有函数名的函数只能在包级语法块中被声明 4.函数值( ...
- 【问题&解决】sql2012安装时卡在正在启动操作系统功能"NetFx3"上不动的解决办法
安装完windows8 后开始安装sql2012,安装过程中停在“正在启动操作系统功能"NetFx3"”不动了,很是着急,于是上网查了一下资料,原来NetFx3指的是Framewo ...
- 孙鑫视频学习:VS2010中找不到【Tab order】菜单项
在学习孙鑫视频中,修改Tab顺序时,找不到VC6.0中提到的[Layout]->[Tab order]菜单项,但VC2010中可以用Ctrl+D调出来Tab顺序,或者[格式]->[Tab键 ...