Skip to content

Commit 314621b

Browse files
author
Dhananjay Nagargoje
committed
max connected regions
1 parent 5184d5c commit 314621b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problems.ongraph;
2+
3+
import java.util.List;
4+
5+
6+
public class MatrixConnectedRegions {
7+
8+
final static int[][] SHIFT = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
9+
10+
11+
public static void flipColor(int x, int y, List<List<Boolean>> image) {
12+
boolean currentColor = image.get(x).get(y);
13+
paint(image, currentColor, x, y);
14+
return;
15+
}
16+
17+
private static void paint(List<List<Boolean>> image, boolean currentColor, int x, int y) {
18+
19+
image.get(x).set(y, !currentColor);
20+
for (int a[] : SHIFT) {
21+
if (isFeasible(image, x + a[0], y + a[1], currentColor)) {
22+
paint(image, currentColor, x + a[0], y + a[1]);
23+
}
24+
}
25+
}
26+
27+
public static boolean isFeasible(List<List<Boolean>> image, int x, int y, boolean currentColor) {
28+
return x >= 0 && x < image.size() && y >= 0
29+
&& y < image.get(x).size()
30+
&& image.get(x).get(y) == currentColor;
31+
32+
}
33+
34+
35+
}

0 commit comments

Comments
 (0)