-
Notifications
You must be signed in to change notification settings - Fork 0
/
10026.java
71 lines (58 loc) · 1.53 KB
/
10026.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
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
static int N;
static int X[] = {1,-1,0,0};
static int Y[] = {0,0,1,-1};
public static void main(String[] args) throws Exception{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(input.readLine());
int caseA[][] = new int[N][N];
int caseB[][] = new int[N][N];
boolean visitA[][] = new boolean[N][N];
boolean visitB[][] = new boolean[N][N];
for(int i=0; i<N; i++) {
String row = input.readLine();
for(int j=0; j<N; j++) {
char c = row.charAt(j);
// R = 0, G = 1, B = 2
if(c == 'R' || c == 'G') {
caseB[i][j] = 0;
caseA[i][j] = c == 'R' ? 0 : 1;
}else {
caseA[i][j] = 2;
caseB[i][j] = 2;
}
}
}
int x = 0;
int y = 0;
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
if(!visitA[i][j]) {
DFS(i, j, caseA[i][j], caseA, visitA);
x++;
}
if(!visitB[i][j]) {
DFS(i, j, caseB[i][j], caseB, visitB);
y++;
}
}
}
System.out.println(x+" "+y);
}
static void DFS(int x, int y, int key, int matrix[][], boolean visit[][]) {
visit[x][y] = true;
for(int i=0; i<4; i++){
int nx = x + X[i];
int ny = y + Y[i];
if (isPossible(nx, ny, key, matrix, visit)){
DFS(nx, ny, key, matrix, visit);
}
}
}
static boolean isPossible(int x, int y, int key, int matrix[][], boolean visit[][]) {
return (x>-1 && y>-1 && x<N && y<N) && !visit[x][y]
&& matrix[x][y] == key ? true : false;
}
}