Skip to content

Commit

Permalink
[Gold V] Title: 적록색약, Time: 316 ms, Memory: 19956 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
dukbong committed Dec 18, 2023
1 parent 0e8a7c5 commit f4f0c48
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 백준/Gold/10026. 적록색약/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# [Gold V] 적록색약 - 10026

[문제 링크](https://www.acmicpc.net/problem/10026)

### 성능 요약

메모리: 19956 KB, 시간: 316 ms

### 분류

너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색

### 제출 일자

2023년 12월 18일 13:25:11

### 문제 설명

<p>적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다.</p>

<p>크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록), B(파랑) 중 하나를 색칠한 그림이 있다. 그림은 몇 개의 구역으로 나뉘어져 있는데, 구역은 같은 색으로 이루어져 있다. 또, 같은 색상이 상하좌우로 인접해 있는 경우에 두 글자는 같은 구역에 속한다. (색상의 차이를 거의 느끼지 못하는 경우도 같은 색상이라 한다)</p>

<p>예를 들어, 그림이 아래와 같은 경우에</p>

<pre>RRRBB
GGBBB
BBBRR
BBRRR
RRRRR</pre>

<p>적록색약이 아닌 사람이 봤을 때 구역의 수는 총 4개이다. (빨강 2, 파랑 1, 초록 1) 하지만, 적록색약인 사람은 구역을 3개 볼 수 있다. (빨강-초록 2, 파랑 1)</p>

<p>그림이 입력으로 주어졌을 때, 적록색약인 사람이 봤을 때와 아닌 사람이 봤을 때 구역의 수를 구하는 프로그램을 작성하시오.</p>

### 입력

<p>첫째 줄에 N이 주어진다. (1 ≤ N ≤ 100)</p>

<p>둘째 줄부터 N개 줄에는 그림이 주어진다.</p>

### 출력

<p>적록색약이 아닌 사람이 봤을 때의 구역의 개수와 적록색약인 사람이 봤을 때의 구역의 수를 공백으로 구분해 출력한다.</p>

101 changes: 101 additions & 0 deletions 백준/Gold/10026. 적록색약/적록색약.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

static int t;

static int maxY, maxX;
static String[][] graph;
static boolean[][] visitedC;
static boolean[][] visitedS;

static int[] moveY = { -1, 1, 0, 0 };
static int[] moveX = { 0, 0, -1, 1 };

public static void main(String[] args) throws IOException {
new Main().solution();
}

private void solution() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
maxY = Integer.parseInt(br.readLine());
int commonColor = 0;
int specialColor = 0;

// graph Size and Value fixed
graph = new String[maxY][];

for (int i = 0; i < maxY; i++) {
graph[i] = br.readLine().split("");
}
maxX = graph[0].length;

visitedC = new boolean[maxY][maxX];
visitedS = new boolean[maxY][maxX];

for (t = 0; t < 2; t++) {
for (int i = 0; i < maxY; i++) {
for (int j = 0; j < maxX; j++) {
if (t == 0) {
if (!visitedC[i][j]) {
bfs(i, j, visitedC);
commonColor++;
}
} else {
if (!visitedS[i][j]) {
bfs(i, j, visitedS);
specialColor++;
}
}
}
}
}
System.out.println(commonColor + " " + specialColor);
}

private void bfs(int y, int x, boolean[][] visited) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] { y, x });
visitedC[y][x] = true;

// 적록색명
if (t == 1) {
for(int i = 0; i < maxY; i++){
for(int j = 0; j < maxX; j++){
if(graph[i][j].equals("G")){
graph[i][j] = "R";
}
}
}
}

while (!queue.isEmpty()) {
int[] now = queue.poll();
String nowColor = graph[now[0]][now[1]];
for (int i = 0; i < 4; i++) {
int nextY = now[0] + moveY[i];
int nextX = now[1] + moveX[i];

if (nextY >= 0 && nextY < maxY && nextX >= 0 && nextX < maxX) {
if (!visited[nextY][nextX]) {
if (nowColor.equals(graph[nextY][nextX])) {
queue.offer(new int[] { nextY, nextX });
visited[nextY][nextX] = true;
}

}
}
}
}
}
}

/*
*
* RRRBB GGBBB BBBRR BBRRR RRRRR
*
*/

0 comments on commit f4f0c48

Please sign in to comment.