Skip to content

Commit 4af6693

Browse files
committed
[Gold V] Title: 적록색약, Time: 168 ms, Memory: 12336 KB -BaekjoonHub
1 parent f75777c commit 4af6693

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [Gold V] 적록색약 - 10026
2+
3+
[문제 링크](https://www.acmicpc.net/problem/10026)
4+
5+
### 성능 요약
6+
7+
메모리: 12336 KB, 시간: 168 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2025년 3월 17일 10:03:06
16+
17+
### 문제 설명
18+
19+
<p>적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다.</p>
20+
21+
<p>크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록), B(파랑) 중 하나를 색칠한 그림이 있다. 그림은 몇 개의 구역으로 나뉘어져 있는데, 구역은 같은 색으로 이루어져 있다. 또, 같은 색상이 상하좌우로 인접해 있는 경우에 두 글자는 같은 구역에 속한다. (색상의 차이를 거의 느끼지 못하는 경우도 같은 색상이라 한다)</p>
22+
23+
<p>예를 들어, 그림이 아래와 같은 경우에</p>
24+
25+
<pre>RRRBB
26+
GGBBB
27+
BBBRR
28+
BBRRR
29+
RRRRR</pre>
30+
31+
<p>적록색약이 아닌 사람이 봤을 때 구역의 수는 총 4개이다. (빨강 2, 파랑 1, 초록 1) 하지만, 적록색약인 사람은 구역을 3개 볼 수 있다. (빨강-초록 2, 파랑 1)</p>
32+
33+
<p>그림이 입력으로 주어졌을 때, 적록색약인 사람이 봤을 때와 아닌 사람이 봤을 때 구역의 수를 구하는 프로그램을 작성하시오.</p>
34+
35+
### 입력
36+
37+
<p>첫째 줄에 N이 주어진다. (1 ≤ N ≤ 100)</p>
38+
39+
<p>둘째 줄부터 N개 줄에는 그림이 주어진다.</p>
40+
41+
### 출력
42+
43+
<p>적록색약이 아닌 사람이 봤을 때의 구역의 개수와 적록색약인 사람이 봤을 때의 구역의 수를 공백으로 구분해 출력한다.</p>
44+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const fs = require('fs');
2+
let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
3+
4+
const N = Number(input[0]);
5+
let colorArr = [];
6+
let count = 0;
7+
let count2 = 0;
8+
let visitedArr = [];
9+
let changedColorVisitedArr = [];
10+
let changedColorArr = [];
11+
12+
for (let i = 1; i <= N; i++) {
13+
colorArr[i] = [,];
14+
changedColorArr[i] = [,];
15+
colorArr[i].push(...input[i].split(''));
16+
changedColorArr[i].push(...input[i].split(''));
17+
visitedArr[i] = new Array(N + 1).fill(false);
18+
changedColorVisitedArr[i] = new Array(N + 1).fill(false);
19+
}
20+
21+
// 적록색약 색 변환
22+
for (let i = 1; i <= N; i++) {
23+
for (let j = 1; j <= N; j++) {
24+
let color = changedColorArr[i][j];
25+
if (color == 'G') {
26+
changedColorArr[i][j] = 'R';
27+
}
28+
}
29+
}
30+
31+
const dfs = (x, y, color) => {
32+
if (visitedArr[x][y] || x <= 0 || x > N || y <= 0 || y > N) {
33+
return false;
34+
}
35+
visitedArr[x][y] = true;
36+
37+
if (x - 1 >= 1 && !visitedArr[x - 1][y] && colorArr[x - 1][y] == color) {
38+
dfs(x - 1, y, color);
39+
}
40+
if (x + 1 <= N && !visitedArr[x + 1][y] && colorArr[x + 1][y] == color) {
41+
dfs(x + 1, y, color);
42+
}
43+
if (y - 1 >= 1 && !visitedArr[x][y - 1] && colorArr[x][y - 1] == color) {
44+
dfs(x, y - 1, color);
45+
}
46+
if (y + 1 <= N && !visitedArr[x][y + 1] && colorArr[x][y + 1] == color) {
47+
dfs(x, y + 1, color);
48+
}
49+
return true;
50+
};
51+
52+
const dfs2 = (x, y, color) => {
53+
if (changedColorVisitedArr[x][y] || x <= 0 || x > N || y <= 0 || y > N) {
54+
return false;
55+
}
56+
changedColorVisitedArr[x][y] = true;
57+
58+
if (x - 1 >= 1 && !changedColorVisitedArr[x - 1][y] && changedColorArr[x - 1][y] == color) {
59+
dfs2(x - 1, y, color);
60+
}
61+
if (x + 1 <= N && !changedColorVisitedArr[x + 1][y] && changedColorArr[x + 1][y] == color) {
62+
dfs2(x + 1, y, color);
63+
}
64+
if (y - 1 >= 1 && !changedColorVisitedArr[x][y - 1] && changedColorArr[x][y - 1] == color) {
65+
dfs2(x, y - 1, color);
66+
}
67+
if (y + 1 <= N && !changedColorVisitedArr[x][y + 1] && changedColorArr[x][y + 1] == color) {
68+
dfs2(x, y + 1, color);
69+
}
70+
return true;
71+
};
72+
73+
for (let i = 1; i <= N; i++) {
74+
for (let j = 1; j <= N; j++) {
75+
let color = colorArr[i][j];
76+
if (dfs(i, j, color)) {
77+
count++;
78+
}
79+
let color2 = changedColorArr[i][j];
80+
if (dfs2(i, j, color2)) {
81+
count2++;
82+
}
83+
}
84+
}
85+
86+
console.log(count, count2);
87+
88+
// 빨, 파, 초
89+
// 빨초, 파
90+
// 모든 좌표에 대해 DFS 실행
91+
// 상하좌우에 같은 문자열이 있는지 확인하는 로직
92+
// visited

0 commit comments

Comments
 (0)