-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColoringBook.java
59 lines (55 loc) · 1.73 KB
/
ColoringBook.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Programmers.Algorithm;
public class ColoringBook {
int [][] picture;
boolean [][] isGroup;
public int makeGroup(int i, int j, int prev) {
if(picture[i][j] != prev || isGroup[i][j]) return 0;
isGroup[i][j] = true;
int count = 1;
if(i+1 < picture.length) count += makeGroup(i+1, j, prev);
if(i-1 >= 0) count += makeGroup(i-1, j, prev);
if(j+1 < picture[0].length) count += makeGroup(i, j+1, prev);
if(j-1 >= 0) count += makeGroup(i, j-1, prev);
return count;
}
public int[] solution(int m, int n, int[][] picture) {
int numberOfArea = 0;
int maxSizeOfOneArea = 0;
this.picture = picture;
isGroup = new boolean[m][n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
int groupNum = picture[i][j];
int count = 0;
if(!isGroup[i][j] && groupNum != 0) {
count = makeGroup(i, j, groupNum);
numberOfArea++;
}
maxSizeOfOneArea = Math.max(maxSizeOfOneArea, count);
}
}
int [] answer = {numberOfArea, maxSizeOfOneArea};
return answer;
}
public static void main(String[] args) {
ColoringBook test = new ColoringBook();
int [][] picture = {
{0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0},
{0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,1,1,1,1,2,1,1,1,1,2,1,1,1,1,0},
{0,1,1,1,2,1,2,1,1,2,1,2,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,3,3,3,1,1,1,1,1,1,3,3,3,1,0},
{0,1,1,1,1,1,2,1,1,2,1,1,1,1,1,0},
{0,0,1,1,1,1,1,2,2,1,1,1,1,1,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
};
int m = 13, n = 16;
int [] answer = test.solution(m, n, picture);
System.out.println(answer[0] + " " + answer[1]);
}
}