Skip to content

Commit 7fced5d

Browse files
committed
85차 2번 문제풀이
1 parent 9587c01 commit 7fced5d

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

live8/test85/문제2/이상민.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const input = require("fs")
2+
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
3+
.toString()
4+
.trim()
5+
.split("\n");
6+
7+
function bfs(x, y, visited, map) {
8+
const queue = [];
9+
const dx = [0, 1, 0, -1];
10+
const dy = [1, 0, -1, 0];
11+
12+
queue.push([x, y]);
13+
14+
while (queue.length) {
15+
const [currX, currY] = queue.shift();
16+
17+
for (let i = 0; i < 4; i++) {
18+
const newX = currX + dx[i];
19+
const newY = currY + dy[i];
20+
if (newX >= 0 && newX < map[0].length && newY >= 0 && newY < map.length) {
21+
if (map[newY][newX] === "#" && !visited[newY][newX]) {
22+
visited[newY][newX] = true;
23+
queue.push([newX, newY]);
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
function solution(h, w, map) {
31+
const visited = Array.from({ length: h }, () => Array(w).fill(false));
32+
let count = 0;
33+
34+
for (let i = 0; i < h; i++) {
35+
for (let j = 0; j < w; j++) {
36+
if (map[i][j] === "#" && !visited[i][j]) {
37+
bfs(j, i, visited, map);
38+
count++;
39+
}
40+
}
41+
}
42+
43+
return count;
44+
}
45+
46+
const tmp = [];
47+
let T = 1;
48+
49+
while (true) {
50+
if (T >= input.length) break;
51+
const [w, h] = input[T].split(" ").map(Number);
52+
const map = [];
53+
for (let i = T + 1; i <= T + w; i++) {
54+
map.push(input[i]);
55+
}
56+
tmp.push([w, h, map]);
57+
T += w + 1;
58+
}
59+
60+
const answer = [];
61+
62+
for (const t of tmp) {
63+
answer.push(solution(...t));
64+
}
65+
66+
console.log(answer.join("\n"));

0 commit comments

Comments
 (0)