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中的油桶工具,就是给指定的位置填充颜色,如果要填充的颜色和原来的颜色相同,则不发生变换,如果不同的话,则把相连接的区域都填充为新的颜色。那么我们使用递归来做,首先判断要填充的颜色和该位置上原有的颜色是否相同,如果不同的话就开始填充,如果周围颜色和起始位置颜色相同也填充,参见代码如下:

  1. enum Color { Black, White, Red, Yellow, Green };
  2.  
  3. class Solution {
  4. public:
  5. bool paintFill(vector<vector<Color> > &screen, int x, int y, Color newColor) {
  6. if (screen[x][y] == newColor) return false;
  7. return paintFill(screen, x, y, screen[x][y], newColor);
  8. }
  9. bool paintFill(vector<vector<Color> > &screen, int x, int y, Color oldColor, Color newColor) {
  10. if (x < || x >= screen.size() || y < || y >= screen[].size()) return false;
  11. if (screen[x][y] == oldColor) {
  12. screen[x][y] = newColor;
  13. paintFill(screen, x - , y, oldColor, newColor);
  14. paintFill(screen, x + , y, oldColor, newColor);
  15. paintFill(screen, x, y - , oldColor, newColor);
  16. paintFill(screen, x, y + , oldColor, newColor);
  17. }
  18. return true;
  19. }
  20. };

[CareerCup] 9.7 Paint Fill 填充的更多相关文章

  1. [Swift通天遁地]六、智能布局-(5)给视图添加Align(对齐)和Fill(填充的约束以及Label的约束

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. js--数组的 fill() 填充方法详解

    前言 我们知道了很多了初始化数组的方法,但是初始化数组之后,数组中的每一项元素默认为 empty 空位占位,如何对数组这些空位添加默认的元素,ES6提供了 fill() 方法实现这一操作.本文总结数组 ...

  3. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  4. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  5. HTML5 Canvas ( 填充图形的绘制 ) closePath, fillStyle, fill

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. fill 函数

    fill函数的作用是:将一个区间的元素都赋予val值.函数参数:fill(first,last,val);//first为容器的首迭代器,last为容器的末迭代器,val为将要替换的值. 例题:给你n ...

  7. 绘图——Android绘图基础:Canvas、Paint等

    Android的绘图应该继承View组件,并重写它的onDraw(Canvas canvas)方法即可. 重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Canv ...

  8. Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解

    上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...

  9. Paint的基本使用

    代码地址如下:http://www.demodashi.com/demo/14712.html 前言 在讲述自定义控件的时候,我们讲到了自定义控件的基本步骤,那么在自定义控件中,我们第一个需要了解的就 ...

随机推荐

  1. IOS的UI基础02

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. 那些Xcode不能错过的插件

    转载来自网络   古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战斗力的绝佳途径!以下是搜集的一些有力的XCode插件.   1.全能搜索家CodePilot 2.0 你要找的 ...

  3. Swift面向对象基础(中)——Swift中的方法

    学习来自<极客学院> 1.定义方法需要在类型(枚举,结构体,类)里定义,不能独立定义,独立定义的是函数 2.方法要么属于该类型本身,要么是该类型的一个实例 3.不能独立执行方法,执行方法必 ...

  4. android多种布局的列表实现

    最近有一个列表效果,需要一个列表有多种布局,最终效果如下: 这个我也问了同事以及开发群里的朋友,居然都没得到最优的实现方式的回答,看来这种复杂列表的需求还是比较少的,我自己也走了一些弯路,把我几个实现 ...

  5. python sorted

    python列表排序 python字典排序 sorted List的元素可以是各种东西,字符串,字典,自己定义的类等. sorted函数用法如下: sorted(data, cmp=None, key ...

  6. Effective Java 69 Prefer concurrency utilities to wait and notify

    Principle Use the higher-level concurrency utilities instead of wait and notify for easiness. Use Co ...

  7. JavaScript Patterns 2.11 Writing Comments

    Document all functions, their arguments and return values, and also any interesting or unusual algor ...

  8. 8种Nosql数据库系统对比

    导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型NoSQL数据库的文章. 虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只 ...

  9. centos7 + php7 lamp全套最新版本配置,还有mongodb和redis

    我是个懒人,能yum就yum啦 所有软件的版本一直会升级,注意自己当时的版本是不是已经更新了. 首先装centos7 如果你忘了设置swap分区,下面的文章可以教你怎么补一个上去: http://ww ...

  10. web.xml文件报错:The processing instruction target matching "[xX][mM][lL]" is not allowed.

    昨晚把我的项目上传到了gitlab,然后今天在公司从gitlab下载下来, 发现web.xml报错.