728x90
반응형
문제
제출한 답안
package BOJ;
import java.util.Scanner;
public class BOJ_10026 {
private static String[][] colors;
private static boolean[][] isVisited;
private static int[] dx = {1,-1,0,0};
private static int[] dy = {0,0,1,-1};
private static int M;
// R: Red
// G: Green
// B: Blue
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
M = Integer.parseInt(sc.nextLine());
colors = new String[1001][1001];
isVisited = new boolean[1001][1001];
for (int i = 0; i < M; i++) {
String[] split = sc.nextLine().split("");
for (int j = 0; j < M; j++) {
colors[i][j] = split[j];
}
}
sc.close();
int normal = 0;
// normal
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (!isVisited[i][j]){
dfs(i,j);
normal++;
}
}
}
isVisited = new boolean[1001][1001];
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (colors[i][j].equals("G")) colors[i][j] = "R";
}
}
int abnormal = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (!isVisited[i][j]){
dfs(i,j);
abnormal++;
}
}
}
System.out.println(normal + " " + abnormal);
}
private static void dfs(int x, int y){
isVisited[x][y] = true;
String color = colors[x][y];
for (int i = 0; i < dx.length; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= M || ny >= M || isVisited[nx][ny] || !color.equals(colors[nx][ny])) continue;
isVisited[nx][ny] = true;
dfs(nx,ny);
}
}
}
728x90
반응형
'Algorithm' 카테고리의 다른 글
[BOJ 2468] 안전 영역 ( with Java ) (0) | 2023.04.16 |
---|---|
[BOJ 4963] 섬의 개수 ( with Java ) (0) | 2023.04.16 |
[BOJ 11724] 연결 요소의 개수 ( with Java ) (0) | 2023.04.16 |
[BOJ 1012] 유기농 배추 ( with Java ) (0) | 2023.04.16 |
[BOJ 2667] 단지번호붙이기 ( with Java ) (0) | 2023.04.16 |