-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay32.java
More file actions
44 lines (39 loc) · 1.58 KB
/
Day32.java
File metadata and controls
44 lines (39 loc) · 1.58 KB
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
37
38
39
40
41
42
43
44
public class Day32 {
public static int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int originalColor = image[sr][sc];
if (originalColor == newColor) {
return image;
}
fill(image, sr, sc, originalColor, newColor);
return image;
}
private static void fill(int[][] image, int row, int col, int originalColor, int newColor) {
if (row < 0 || row >= image.length || col < 0 || col >= image[0].length || image[row][col] != originalColor) {
return;
}
image[row][col] = newColor;
fill(image, row - 1, col, originalColor, newColor); // Up
fill(image, row + 1, col, originalColor, newColor); // Down
fill(image, row, col - 1, originalColor, newColor); // Left
fill(image, row, col + 1, originalColor, newColor); // Right
}
public static void main(String[] args) {
int[][] image1 = {{1, 1, 1}, {1, 1, 0}, {1, 0, 1}};
int sr1 = 1, sc1 = 1, color1 = 2;
int[][] result1 = floodFill(image1, sr1, sc1, color1);
printImage(result1);
int[][] image2 = {{0, 0, 0}, {0, 0, 0}};
int sr2 = 0, sc2 = 0, color2 = 0;
int[][] result2 = floodFill(image2, sr2, sc2, color2);
printImage(result2);
}
private static void printImage(int[][] image) {
for (int[] row : image) {
for (int pixel : row) {
System.out.print(pixel + " ");
}
System.out.println();
}
System.out.println();
}
}