-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ10026.java
More file actions
76 lines (66 loc) · 2.02 KB
/
BOJ10026.java
File metadata and controls
76 lines (66 loc) · 2.02 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class BOJ10026 {
static int N;
static char[][] arr;
static int[] dx = {0,0,-1,1};
static int[] dy = {-1,1,0,0};
static boolean[][] visited;
static String str;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new char[N+1][N+1];
visited = new boolean[N+1][N+1];
for (int i = 0; i < N; i++) {
str = br.readLine();
for (int j = 0; j < N; j++) {
arr[i][j] = str.charAt(j);
}
}
int count = 0;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(!visited[i][j]){
dfs(i, j);
count++;
}
}
}
System.out.print(count);
count = 0;
visited = new boolean[N+1][N+1];
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(arr[i][j] == 'G'){
arr[i][j] = 'R';
}
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(!visited[i][j]){
dfs(i, j);
count++;
}
}
}
System.out.print(" "+count);
}
public static void dfs(int n, int m){
visited[n][m] = true;
char tmp = arr[n][m];
for(int i = 0; i < 4; i++){
int newx = dx[i] + n;
int newy = dy[i] + m;
if(newx < 0 || newy < 0 || newx > N || newy > N){
continue;
}
if(!visited[newx][newy] && arr[newx][newy] == tmp){
dfs(newx, newy);
}
}
}
}