import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static class XY{
int x; int y;
public XY(int x, int y){
this.x = x; this.y = y;
}
}
static int N;
static int[] dx = {-1,1,0,0};
static int[] dy = {0,0,-1,1};
static boolean[][] visited;
static char[][] map;
static int ans = 0;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
visited = new boolean[N][N];
map = new char[N][N];
for(int i=0; i<N; i++){
String str = br.readLine();
for(int j=0; j<N; j++){
map[i][j] = str.charAt(j);
}
}
pre_bfs();
System.out.println(ans);
//초기화
ans = 0;
visited = new boolean[N][N]; //초기화
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(map[i][j] == 'R')
map[i][j] = 'G';
}
}
pre_bfs();
System.out.println(ans);
}
private static void pre_bfs(){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(!visited[i][j]){
bfs(i,j);
ans++;
}
}
}
}
private static void bfs(int a, int b){
Queue<XY> q = new LinkedList<>();
q.offer(new XY(a,b));
visited[a][b] = true;
while(!q.isEmpty()){
int x = q.peek().x;
int y = q.peek().y;
q.poll();
for(int i=0; i<4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(xx>=0&&xx<N && yy>=0&&yy<N){
if(map[x][y]==map[xx][yy] && !visited[xx][yy])
bfs(xx, yy);
}
}
}
}
}
BFS탐색 - 초기화, R->G로 변경 - BFS탐색 순이다.
'1d-1c > BOJ' 카테고리의 다른 글
7562_나이트의 이동 (JAVA) (0) | 2020.11.18 |
---|---|
2529_부등호 (JAVA) (0) | 2020.11.17 |
10819_차이를 최대로 (JAVA) (0) | 2020.11.15 |
15663~15666_ N과 M (9)~(12) (JAVA) (0) | 2020.11.13 |
15654~15657_N과 M (5)~(8) (JAVA) (0) | 2020.11.11 |