-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_10026.java
More file actions
75 lines (67 loc) · 2.09 KB
/
boj_10026.java
File metadata and controls
75 lines (67 loc) · 2.09 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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class boj_10026 {
static int N;
static char[][] arr;
static int[] dx={-1,1,0,0};
static int[] dy={0,0,-1,1};
static boolean[][] visit;
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][N];
for(int i=0;i<N;i++){
String s= br.readLine();
for(int j=0;j<N;j++){
arr[i][j] = s.charAt(j);
}
}
visit=new boolean[N][N];
int answer1=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(!visit[i][j]){
bfs(i,j,arr[i][j]);
answer1++;
}
//다음 반복문에서 적록색맹 탐색을 위해 색을 바꾸어줌
if(arr[i][j]=='G'){
arr[i][j]='R';
}
}
}
visit=new boolean[N][N];
int answer2=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(!visit[i][j]){
bfs(i,j,arr[i][j]);
answer2++;
}
}
}
System.out.println(answer1+" "+answer2);
}
private static void bfs(int x,int y,char target){
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x,y});
visit[x][y]=true;
while(!queue.isEmpty()){
int[] cur=queue.poll();
int curx=cur[0],cury=cur[1];
for(int i=0;i<4;i++){
int nx=curx+dx[i];
int ny=cury+dy[i];
if(nx<0 || ny<0 || nx>=N || ny>=N || visit[nx][ny]) continue;
if(!visit[nx][ny] && arr[nx][ny]==target){
visit[nx][ny]=true;
queue.add(new int[]{nx,ny});
}
}
}
}
}