Skip to content

Commit 4a52572

Browse files
committed
[Gold V] Title: 적록색약, Time: 164 ms, Memory: 11408 KB -BaekjoonHub
1 parent 873a6e7 commit 4a52572

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

백준/Gold/10026. 적록색약/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 34112 KB, 시간: 80 ms
7+
메모리: 11408 KB, 시간: 164 ms
88

99
### 분류
1010

11-
너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색, 격자 그래프
1212

1313
### 제출 일자
1414

15-
2024년 4월 8일 14:15:25
15+
2026년 2월 19일 17:12:04
1616

1717
### 문제 설명
1818

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// /dev/stdin
2+
const fs = require("fs");
3+
let input = fs
4+
.readFileSync("/dev/stdin")
5+
.toString()
6+
.split("\n")
7+
.map((x) => x.replace("\r", ""));
8+
9+
const N = Number(input[0]);
10+
let answer = "";
11+
const board = [];
12+
for (let i = 1; i <= N; i++) {
13+
board.push(input[i].split(""));
14+
}
15+
16+
let visited = Array.from({ length: N + 1 }, () => Array(N).fill(false));
17+
let result = 0;
18+
for (let i = 0; i < N; i++) {
19+
for (let j = 0; j < N; j++) {
20+
if (!visited[i][j]) {
21+
result++;
22+
dfs(i, j, board[i][j]);
23+
}
24+
}
25+
}
26+
answer = result + " ";
27+
28+
function dfs(x, y, color) {
29+
visited[x][y] = true;
30+
// 상
31+
if (x - 1 >= 0 && board[x - 1][y] === color && !visited[x - 1][y]) {
32+
dfs(x - 1, y, color);
33+
}
34+
// 하
35+
if (x + 1 < N && board[x + 1][y] === color && !visited[x + 1][y]) {
36+
dfs(x + 1, y, color);
37+
}
38+
// 좌
39+
if (y - 1 >= 0 && board[x][y - 1] === color && !visited[x][y - 1]) {
40+
dfs(x, y - 1, color);
41+
}
42+
// 우
43+
if (y + 1 < N && board[x][y + 1] === color && !visited[x][y + 1]) {
44+
dfs(x, y + 1, color);
45+
}
46+
}
47+
48+
for (let i = 0; i < N; i++) {
49+
for (let j = 0; j < N; j++) {
50+
if (board[i][j] === "G") {
51+
board[i][j] = "R";
52+
}
53+
}
54+
}
55+
56+
visited = Array.from({ length: N + 1 }, () => Array(N).fill(false));
57+
58+
result = 0;
59+
for (let i = 0; i < N; i++) {
60+
for (let j = 0; j < N; j++) {
61+
if (!visited[i][j]) {
62+
result++;
63+
dfs(i, j, board[i][j]);
64+
}
65+
}
66+
}
67+
answer = answer + result;
68+
console.log(answer);

0 commit comments

Comments
 (0)