[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 < 0 || x >= screen.size() || y < 0 || y >= screen[0].size()) return false;
        if (screen[x][y] == oldColor) {
            screen[x][y] = newColor;
            paintFill(screen, x - 1, y, oldColor, newColor);
            paintFill(screen, x + 1, y, oldColor, newColor);
            paintFill(screen, x, y - 1, oldColor, newColor);
            paintFill(screen, x, y + 1, oldColor, newColor);
        }    
        return true;
    }
};

本文转自博客园Grandyang的博客,原文链接:填充[CareerCup] 9.7 Paint Fill,如需转载请自行联系原博主。

相关文章
|
6月前
|
编解码 UED
媒体查询中 max-width 和 min-width 属性的作用
【10月更文挑战第24天】可以结合其他媒体特性,如高度、分辨率等,来进一步优化和定制媒体查询的效果。通过对这些属性的深入理解和灵活运用,我们能够更好地应对不同屏幕尺寸带来的挑战,打造出适应各种环境的优秀设计作品。
|
7月前
|
前端开发
min-width/max-width 和 min-height/max-height 属性间的覆盖规则
在CSS中,min-width/max-width及min-height/max-height属性用于控制元素的最小和最大尺寸。当min-width与max-width或min-height与max-height属性同时设置时,若它们的值有冲突,max-width和max-height具有更高的优先级。这意味着元素的实际宽度和高度将受限于max-width和max-height的规定,而min-width和min-height则确保了元素不会小于所设定的最小尺寸。
|
6月前
|
前端开发 容器
max-height 属性和 min-height 属性有什么区别?
【10月更文挑战第27天】`max-height`属性和`min-height`属性在控制元素高度方面各有特点和适用场景,通过合理地运用这两个属性,可以更灵活地实现各种页面布局效果,满足不同的设计和功能需求。
定义一个类Rectangle,描述一个矩形,包含有长、宽两种属性(用length和width表示)和计算面积的方法(方面名定义为area)。
定义一个类Rectangle,描述一个矩形,包含有长、宽两种属性(用length和width表示)和计算面积的方法(方面名定义为area)。
928 0
ListView列宽自适应,设置ListView.Column[0].Width := -1;
使用TListView列表显示内容,如果列内容过长,就会显示成‘XXX…’形式,此时如果双击列标题,列宽将变为自适应。用代码设置如下: 1、设置ListView.Column[0].Width := -1;//列宽根据列内容自适应,此时保证列内容都可见。
3173 0

热门文章

最新文章

下一篇
oss创建bucket