[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 前言 在讲述自定义控件的时候,我们讲到了自定义控件的基本步骤,那么在自定义控件中,我们第一个需要了解的就 ...
随机推荐
- iOS支付宝集成时遇到的问题整理(1)
1.模拟器上运行正常,但是真机调试时报编译链接错误 :ld: '/Users/wangqipeng/Desktop/iOS支付宝官方文档/AlixPayDemo/libcrypto.a(bio_lib ...
- centos到底下载哪个版本?
CentOS-7.0-1406-x86_64-DVD.iso 标准安装版,一般下载这个就可以了CentOS-7.0-1406-x86_64-NetInstall.iso ...
- spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明
一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 struts2.3 ...
- SAM4E单片机之旅——20、DMAC之使用Multi-buffer进行内存拷贝
这次使用这个DMAC的Multi-buffer传输功能,将两个缓冲区的内容拷贝至一个连续的缓冲区中. 一. DMAC 在M4中,DMA控制器(DMAC)比外设DMA控制器(PDC)要复杂,但是功能更加 ...
- Linux Shell 04 数字/字符串/文件测试
一. 数字测试 格式:n1 -op n2 测试操作op: eq/ne/le/ge/lt/gt --> 等于/不等于/小于等于/大于等于/小于/大于 1. 数字比较可以使用特殊的( ...
- 问题解决——Win7 64 安装 AutoCAD 2010 32位 和 清华天河PC CAD
最近单位组了一台电脑,配置还好,E3大法+R9 280,装了Win7 64位系统. ========================================================== ...
- 关于Math类的round、floor、ceil三个方法
一.Math类这三个方法的简介 1.round():取最接近的值. 对于这个方法,查看源代码,其实现如下: public static long round(double a) { if (a != ...
- solrcloud 配置实践
1.环境 3台虚拟机:192.168.26.129.192.168.26.131.192.168.26.132,使用命令sudo iptables -F 关闭防火墙 Solr: solr-6.1.0 ...
- jQuery 常用的代码片段
1. 官网下载链接: http://jquery.com/download/ 2. 引入jquery 文件方式: (1) <script src="http://libs.baid ...
- Shell basic1
A shell script is a text file that typically begins with a shebang, as follows: #!/bin/bash /bin/bas ...