-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ10026.java
More file actions
97 lines (84 loc) · 2.71 KB
/
Copy pathJ10026.java
File metadata and controls
97 lines (84 loc) · 2.71 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package TIL;
import java.io.*;
import java.util.*;
public class J10026 {
static char[][] map;
static char[][] map2;
static int N;
static int[] dx = new int[]{1, 0, -1, 0};
static int[] dy = new int[]{0, 1, 0, -1};
static boolean[][] visited;
static int cnt1;
static int cnt2;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(buffer.readLine());
map = new char[N][N];
map2 = new char[N][N];
visited = new boolean[N][N];
for(int i = 0; i < N; i++){
String str = buffer.readLine();
map[i] = str.toCharArray();
}
for(int i = 0; i < N; i++){
for(int j = 0 ; j<N; j++){
map2[i][j] = map[i][j];
if(map[i][j] == 'R') map2[i][j] = 'G';
}
}
cnt1 = 0;
for(int i = 0 ; i < N; i++){
for(int j = 0 ; j < N; j++){
if(!visited[i][j]) BFS1(i,j);
}
}
visited = new boolean[N][N];
cnt2 = 0;
for(int i = 0 ; i < N; i++){
for(int j = 0 ; j < N; j++){
if(!visited[i][j]) BFS2(i,j);
}
}
System.out.println(cnt1 +" " + cnt2);
}
public static void BFS1(int x, int y){
Queue<int[]> queue = new ArrayDeque<>();
queue.add(new int[]{x, y});
while(!queue.isEmpty()){
int[] dir = queue.poll();
for(int d = 0; d < 4; d++){
int nx = dir[0] + dx[d];
int ny = dir[1] + dy[d];
if(isIn(nx,ny)){
if(map[dir[0]][dir[1]] == map[nx][ny] && !visited[nx][ny]){
visited[nx][ny] = true;
queue.offer(new int[]{nx,ny});
}
}
}
}
cnt1++;
}
public static void BFS2(int x, int y){
Queue<int[]> queue = new ArrayDeque<>();
queue.add(new int[]{x, y});
while(!queue.isEmpty()){
int[] dir = queue.poll();
for(int d = 0; d < 4; d++){
int nx = dir[0] + dx[d];
int ny = dir[1] + dy[d];
if(isIn(nx,ny)){
if(map2[dir[0]][dir[1]] == map2[nx][ny] && !visited[nx][ny]){
visited[nx][ny] = true;
queue.offer(new int[]{nx,ny});
}
}
}
}
cnt2++;
}
public static boolean isIn(int x, int y){
if(x >= 0 && y >= 0 && x < N && y < N) return true;
return false;
}
}