Skip to content

Commit e35a4aa

Browse files
[BOJ] 21938 영상처리 (S2)
1 parent e1927fc commit e35a4aa

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

서정우/12주차/260316.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const fs = require("fs");
2+
const filePath = process.platform === "linux" ? "/dev/stdin" : "./서정우/input.txt";
3+
const input = fs
4+
.readFileSync(filePath)
5+
.toString()
6+
.trim()
7+
.split("\n")
8+
.map((el) => el.trim());
9+
10+
const [N, M] = input[0].split(" ").map(Number);
11+
const displayRGB = input.slice(1, N + 1).map((el) => el.split(" ").map(Number));
12+
const threshold = Number(input[N + 1]);
13+
14+
const display = Array.from({ length: N }, () => new Array(M).fill(0));
15+
16+
for (let i = 0; i < displayRGB.length; i++) {
17+
const row = displayRGB[i];
18+
for (let j = 0; j < row.length / 3; j++) {
19+
display[i][j] =
20+
row.slice(j * 3, j * 3 + 3).reduce((acc, cur) => acc + cur, 0) / 3 >= threshold ? 255 : 0;
21+
}
22+
}
23+
24+
const bfs = (startX, startY) => {
25+
const queue = [[startX, startY]];
26+
const directions = [
27+
[1, 0],
28+
[-1, 0],
29+
[0, 1],
30+
[0, -1],
31+
];
32+
33+
while (queue.length) {
34+
const [x, y] = queue.shift();
35+
for (const [dx, dy] of directions) {
36+
const newX = x + dx;
37+
const newY = y + dy;
38+
if (newX >= 0 && newX < N && newY >= 0 && newY < M && display[newX][newY] === 255) {
39+
display[newX][newY] = 0;
40+
queue.push([newX, newY]);
41+
}
42+
}
43+
}
44+
};
45+
46+
let count = 0;
47+
for (let i = 0; i < N; i++) {
48+
for (let j = 0; j < M; j++) {
49+
if (display[i][j] === 255) {
50+
count++;
51+
display[i][j] = 0;
52+
bfs(i, j);
53+
}
54+
}
55+
}
56+
57+
console.log(count);

서정우/input.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
6 10 12
2-
11 10 17 5 23 28
1+
3 3
2+
255 255 255 100 100 100 255 255 255
3+
100 100 100 255 255 255 100 100 100
4+
255 255 255 100 100 100 255 255 255
5+
101

0 commit comments

Comments
 (0)