Skip to content

Commit d8debbe

Browse files
committed
[Silver I] Title: 단지번호붙이기, Time: 92 ms, Memory: 9480 KB -BaekjoonHub
1 parent fe43bcd commit d8debbe

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

백준/Silver/2667. 단지번호붙이기/README.md

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

55
### 성능 요약
66

7-
메모리: 33284 KB, 시간: 44 ms
7+
메모리: 9480 KB, 시간: 92 ms
88

99
### 분류
1010

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

1313
### 제출 일자
1414

15-
2024년 4월 8일 22:28:05
15+
2026년 2월 13일 12:37:06
1616

1717
### 문제 설명
1818

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

0 commit comments

Comments
 (0)