-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathPaintFill.java
36 lines (33 loc) · 1.11 KB
/
PaintFill.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package chapter08RecursionAndDynamicProgramming;
/**
*
* Problem: 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
}
public class PaintFill {
public boolean paintFill(Color[][] screen, int row, int col, Color nColor) {
if (screen[row][col] == nColor) {
return false;
}
return helper(screen, row, col, screen[row][col], nColor);
}
private boolean helper(Color[][] screen, int row, int col, Color oColor, Color nColor) {
if (row < 0 || row >= screen.length || col < 0 || col >= screen[0].length) {
return false;
}
if (screen[row][col] == oColor) {
screen[row][col] = nColor;
helper(screen, row - 1, col, oColor, nColor); // up
helper(screen, row + 1, col, oColor, nColor); // down
helper(screen, row, col - 1, oColor, nColor); // left
helper(screen, row, col + 1, oColor, nColor); // right
}
return true;
}
}