๋ฌธ์
ํด๋น ํฌ์คํ ์ ๋ฐฑ์ค์ 10026๋ฒ ์ ๋ก์์ฝ ์ ์ ๊ทผ๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ค๋ช ํ ๊ธ ์ ๋๋ค.
์ ๋ต ์์ค ์ฝ๋๋ฅผ ํ์ธํ์๋ ค๋ฉด solve url ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
10026๋ฒ: ์ ๋ก์์ฝ
์ ๋ก์์ฝ์ ๋นจ๊ฐ์๊ณผ ์ด๋ก์์ ์ฐจ์ด๋ฅผ ๊ฑฐ์ ๋๋ผ์ง ๋ชปํ๋ค. ๋ฐ๋ผ์, ์ ๋ก์์ฝ์ธ ์ฌ๋์ด ๋ณด๋ ๊ทธ๋ฆผ์ ์๋ ์ฌ๋์ด ๋ณด๋ ๊ทธ๋ฆผ๊ณผ๋ ์ข ๋ค๋ฅผ ์ ์๋ค. ํฌ๊ธฐ๊ฐ N×N์ธ ๊ทธ๋ฆฌ๋์ ๊ฐ ์นธ์ R(๋นจ๊ฐ), G(์ด๋ก)
www.acmicpc.net
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ด๋ค ๋ฐฉ์์ผ๋ก ์ ๊ทผํด์ผ ํ๋์ง๋ฅผ ๋จผ์ ์๊ฐํด๋ณด์.
๋ฌธ์ ์ ๊ทผ
์ด๋ฒ ๋ฌธ์ ๋ BFS ๋ DFS ๊ฐ์ ๊ทธ๋ํ ํ์์ ๊ตฌํ๋ ฅ์ด ์กฐ๊ธ ์๊ตฌ๋๋ ๋ฌธ์ ์๋ค.
์ฌ์ค BFS๋ DFS ๋ฅผ ์ ๋ค๋ฃฐ ์ค ์๋ค๋ฉด, ํน์ Silver 1 ๋ฌธ์ ๊น์ง ๋งํ์์ด ํ ์ ์๋ค๋ฉด ๊ฐ๋ฒผ์ด ๋ฌธ์ ์๋ค๊ณ ์๊ฐํ๋ค.
ํด๊ฒฐ๋ฒ
์ ๋ก ์์ฝ์ด ๊ตฌ๋ถํ ์ ์๋ ์์ ๋นจ๊ฐ๊ณผ ์ด๋ก์ด๋ ์ ๋ก์์ฝ์ ์์ผ์์ ๋นจ๊ฐ๊ณผ ์ด๋ก์ ํ๋์ ์์ผ๋ก ์ธ์ํ ์ ์๋๋ก ํ๊ณ ์ ์์ธ์ 3๊ฐ์ง ์์ ๋ชจ๋ ๋ค๋ฅด๊ฒ ๊ตฌ๋ถํด์ ์ฐ๊ฒฐ ์์ ์ฐพ๊ธฐ๋ฅผ ์ํํ๋ฉด ๋๋ค.
์ ๋ต ์ฝ๋
public class Main {
private static int[] xPos = {1, -1, 0, 0};
private static int[] yPos = {0, 0, 1, -1};
private static int n;
private static boolean[][] visited;
private static String[][] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
n = Integer.parseInt(br.readLine());
arr = new String[n][n];
for (int i = 0; i < n; i++) {
String[] s = br.readLine().split("");
for (int j = 0; j < n; j++) {
arr[i][j] = s[j];
}
}
int normal = 0;
int colorBlindness = 0;
visited = new boolean[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(!visited[i][j]) { // ๋ฐฉ๋ฌธํ ์ ์ด ์๋ค๋ฉด?
if(arr[i][j].equals("R")) {
visitSingleColor(j, i, "R");
normal++;
}else if(arr[i][j].equals("G")) {
visitSingleColor(j, i, "G");
normal++;
}else if(arr[i][j].equals("B")) {
visitSingleColor(j, i, "B");
normal++;
}
}
}
}
visited = new boolean[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(!visited[i][j]) { // ๋ฐฉ๋ฌธํ ์ ์ด ์๋ค๋ฉด?
if(arr[i][j].equals("R") || arr[i][j].equals("G")) {
visitMultiColor(j, i);
colorBlindness++;
}else if(arr[i][j].equals("B")) {
visitSingleColor(j, i, "B");
colorBlindness++;
}
}
}
}
bw.write(normal + " " + colorBlindness);
bw.flush();
bw.close();
}
private static void visitSingleColor(int x, int y, String color) {
Queue<Position> queue = new LinkedList<>();
queue.add(new Position(x, y));
visited[y][x] = true;
while(!queue.isEmpty()) {
Position front = queue.remove();
for (int i = 0; i < 4; i++) {
int xx = front.x + xPos[i];
int yy = front.y + yPos[i];
if(Position.isValid(xx, yy)) {
if(!visited[yy][xx] && arr[yy][xx].equals(color)) {
visited[yy][xx] = true;
queue.add(new Position(xx, yy));
}
}
}
}
}
private static void visitMultiColor(int x, int y) {
Queue<Position> queue = new LinkedList<>();
queue.add(new Position(x, y));
visited[y][x] = true;
while(!queue.isEmpty()) {
Position front = queue.remove();
for (int i = 0; i < 4; i++) {
int xx = front.x + xPos[i];
int yy = front.y + yPos[i];
if(Position.isValid(xx, yy)) {
if(!visited[yy][xx] && (arr[yy][xx].equals("R") || arr[yy][xx].equals("G"))) {
visited[yy][xx] = true;
queue.add(new Position(xx, yy));
}
}
}
}
}
private static class Position {
int x;
int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
static boolean isValid(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < n;
}
}
}
๋๊ธ