Repository navigation
Expand file tree
/
Copy pathCountCells.java
More file actions
52 lines (45 loc) · 1.65 KB
/
Copy pathCountCells.java
File metadata and controls
52 lines (45 loc) · 1.65 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
45
46
47
48
49
50
51
52
package Recursion;
public class CountCells {
public static final int N = 8;
public static int grid[][] = {
{ 1, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 1, 1, 0, 0, 1, 0, 0 },
{ 1, 1, 0, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 0 },
{ 1, 0, 0, 0, 1, 0, 0, 1 },
{ 0, 1, 1, 0, 0, 1, 1, 1 }
};
// private static int BACKGROUND_COLOR = 0; //not used
private static int IMAGE_COLOR = 1;
private static int ALREADY_COUNTED = 2;
public int countCellsInBlob(int x, int y) {
if (x < 0 || y < 0 || x >= N || y >= N)
return 0;
else if (grid[x][y] != IMAGE_COLOR)
return 0;
else {
grid[x][y] = ALREADY_COUNTED;
return 1 + countCellsInBlob(x - 1, y + 1) + countCellsInBlob(x, y + 1) + countCellsInBlob(x + 1, y + 1)
+ countCellsInBlob(x - 1, y) + countCellsInBlob(x + 1, y)
+ countCellsInBlob(x - 1, y - 1) + countCellsInBlob(x, y - 1) + countCellsInBlob(x + 1, y - 1);
}
}
public static void printGrid() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
CountCells cc = new CountCells();
System.out.println("before counting");
printGrid();
System.out.println("blob size : " + cc.countCellsInBlob(3, 5));
System.out.println("after counting");
printGrid();
}
}