반응형
BOJ 10026 : 적록색약
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int n;
static char[][] color;
static char[][] abnor;
static int[][] delta = { { -1, 0 }, { 1, 0 }, { 0, 1 }, { 0, -1 } };
static int cnt = 0;
public static class Point{
int x;
int y;
char c;
public Point(int x, int y, char c) {
this.x = x;
this.y = y;
this.c = c;
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
color = new char[n][n];
abnor = new char[n][n];
for(int i = 0; i < n; i++) {
String s = br.readLine();
for(int j = 0; j < n; j++) {
color[i][j] = s.charAt(j);
if(color[i][j] == 'G')
abnor[i][j] = 'R';
else
abnor[i][j] = color[i][j];
}
}
int[][] mark = new int[n][n];
Queue<Point> q = new LinkedList<>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(mark[i][j] == 0) {
q.add(new Point(i, j, color[i][j]));
cnt++;
}
while(!q.isEmpty()) {
Point p = q.poll();
mark[p.x][p.y] = 1;
for(int k = 0; k < 4; k++) {
int dx = p.x + delta[k][0];
int dy = p.y + delta[k][1];
if(dx < 0 || dx >= n || dy < 0 || dy >= n) continue;
if(color[dx][dy] == p.c && mark[dx][dy] != 1) {
q.add(new Point(dx, dy, p.c));
mark[dx][dy] = 1;
}
}
}
}
}
System.out.print(cnt + " ");
cnt = 0;
for(int i = 0; i < n; i++)
Arrays.fill(mark[i], 0);
q.clear();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(mark[i][j] == 0) {
q.add(new Point(i, j, abnor[i][j]));
cnt++;
}
while(!q.isEmpty()) {
Point p = q.poll();
mark[p.x][p.y] = 1;
for(int k = 0; k < 4; k++) {
int dx = p.x + delta[k][0];
int dy = p.y + delta[k][1];
if(dx < 0 || dx >= n || dy < 0 || dy >= n) continue;
if(abnor[dx][dy] == p.c && mark[dx][dy] != 1) {
q.add(new Point(dx, dy, p.c));
mark[dx][dy] = 1;
}
}
}
}
}
System.out.println(cnt);
}
}
반응형
'코딩 문제 풀이 > 백준' 카테고리의 다른 글
[백준] 자바 문제 풀이 1351 : 골드5 (0) | 2023.10.08 |
---|---|
[백준] 자바 문제 풀이 13975 : 골드4 (0) | 2023.10.08 |
[백준] 자바 문제 풀이 2014 : 골드1 (0) | 2023.10.06 |
[백준] 자바 문제 풀이 1976 : 골드4 (0) | 2023.10.06 |
[백준] 자바 문제 풀이 17299 : 골드3 (0) | 2023.10.06 |