https://www.acmicpc.net/problem/10026
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int n;
static String s;
static char map[][];
static boolean visits[][];
static int dx[] = {-1,0,0,1};
static int dy[] = {0,1,-1,0};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
map = new char[n+1][n+1];
visits = new boolean[n+1][n+1];
for (int i=0; i<n; i++){
s = sc.next();
for (int j=0; j<n; j++){
map[i][j] = s.charAt(j);
}
}
int cnt = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(!visits[i][j]){
dfs(i,j);
cnt++;
}
}
}
int normal_cnt = cnt;
cnt=0;
visits = new boolean[n+1][n+1];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(map[i][j]=='G'){
map[i][j] = 'R';
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(!visits[i][j]){
dfs(i,j);
cnt++;
}
}
}
int abnormal_cnt = cnt;
System.out.println(normal_cnt + " " + abnormal_cnt);
}
static void dfs(int x, int y){
visits[x][y] = true;
char tmp_char = map[x][y]; // R
for(int i=0; i<4; i++){
int new_x = x+dx[i];
int new_y = y+dy[i];
if (new_x<0 || new_y<0 || new_x>n || new_y>n){
continue;
}
if (!visits[new_x][new_y] && map[new_x][new_y] == tmp_char){
dfs(new_x, new_y);
}
}
}
}
728x90
반응형
'코딩테스트' 카테고리의 다른 글
백준 11654 java (0) | 2023.07.06 |
---|---|
백준 11399 java (0) | 2023.03.21 |
백준 14502 java (0) | 2023.03.21 |
백준 11720 java (0) | 2023.03.21 |
백준 10952 java (0) | 2023.03.21 |