[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 前言 在讲述自定义控件的时候,我们讲到了自定义控件的基本步骤,那么在自定义控件中,我们第一个需要了解的就 ...
随机推荐
- [PL/SQL工具]绿色版PLSQL工具登录时提示初始化失败,无法锁定OCI.dll错误
问题现象:使用绿色版PL/SQL工具进行登录时报如下截图错误: 问题描述:初始化失败,无法锁定oci.dll 解决方法:在PLSQL的菜单栏里依次选择 工具->首选项,在OCI库(自动检测为空) ...
- setInterval setTimeout clearInterval
setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout(). //第一次load的时候就先刷新一次 ...
- easyui-validatebox 验证两次密码是否输入一致
验证两次密码是否输入一致 $.extend($.fn.validatebox.defaults.rules, { /*必须和某个字段相等*/ equalTo: { vali ...
- IE6/7/8不支持jQuery创建非闭合格式的链接A
代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <scri ...
- Semiconnected--强连通缩点
1451: Semiconnected 时间限制: 1 Sec 内存限制: 32 MB 提交: 79 解决: 20 题目描述 For a directed graph G = (V, E), if ...
- Linux下Mysql安装
1.下载安装包 首先查看Linux版本: [root@localhost ~]# lsb_release -a LSB Version: :core-4.0-amd64:core-4.0-noarch ...
- vim自定义配色方案,图文并茂
1.先上图 下面是tcpdump的源码.颜色根据自己的喜好配置,我比较喜欢亮的颜色,看的清楚! 2.下载辅助配置文件 ...
- memcpy函数
实现1:<高质量c++,c编程指南> void *mymemcpy(void *dst,const void *src,size_t num) { assert((dst!=NULL)&a ...
- Hive history date mapping
Hive history table mapping create table fdl_family as select * from (select 'acc1' as account,'famil ...
- Helloworld -SilverN
/*Hello World*/ #include<iostream> #include<cstdio> #include<cstring> using namesp ...