# [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.

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;
}
};

|
1月前
|

18 0
|
10月前
UVa837 - Light and Transparencies(排序)
UVa837 - Light and Transparencies(排序)
41 0
|
C++
【PAT甲级 - C++题解】1033 To Fill or Not to Fill
【PAT甲级 - C++题解】1033 To Fill or Not to Fill
46 0
|
JavaScript 前端开发