From ddbcae70b109d159d69e31e3d6dc6e0e35887696 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Sun, 21 Jul 2024 00:48:41 +0900 Subject: [PATCH] Day20 Jiho 1Q --- ...70\353\266\231\354\235\264\352\270\260.js" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "Jiho/Day20/\353\260\261\354\244\200_BFS_\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260.js" diff --git "a/Jiho/Day20/\353\260\261\354\244\200_BFS_\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260.js" "b/Jiho/Day20/\353\260\261\354\244\200_BFS_\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260.js" new file mode 100644 index 0000000..e44832d --- /dev/null +++ "b/Jiho/Day20/\353\260\261\354\244\200_BFS_\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260.js" @@ -0,0 +1,57 @@ +const input = require("fs") + .readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt") + .toString() + .trim() + .split("\n"); +const N = input.shift(); +const arr = input.map((item) => item.split("").map(Number)); + +const visitedCoords = {}; // 지금까지 방문한 좌표 +const answer = []; +for (let i = 0; i < N; i++) { + for (let j = 0; j < N; j++) { + if (arr[i][j] === 1 && !visitedCoords[[i, j]]) answer.push(bfs(i, j)); + } +} + +// [x,y] 부터 이어지는 번호의 개수를 구하기 +function bfs(x, y) { + const queue = [[x, y]]; + const visited = {}; + visited[[x, y]] = true; + visitedCoords[[x, y]] = true; + let drow = [0, 0, 1, -1]; + let dcol = [-1, 1, 0, 0]; + let cnt = 1; + + while (queue.length) { + for (let i = 0; i < queue.length; i++) { + let [row, col] = queue.shift(); + for (let j = 0; j < 4; j++) { + let nrow = row + drow[j]; + let ncol = col + dcol[j]; + if ( + // 좌표의 유효성 확인 + nrow >= 0 && + ncol >= 0 && + nrow < arr.length && + ncol < arr.length && + // 1일 경우에만 진행되므로 1인 경우만 좌표 출력 + arr[nrow][ncol] === 1 && + // visited 확인 + !visited[[nrow, ncol]] + ) { + visited[[nrow, ncol]] = true; + visitedCoords[[nrow, ncol]] = true; + cnt++; + queue.push([nrow, ncol]); + } + } + } + } + return cnt; +} + +console.log(answer.length); +answer.sort((a, b) => a - b); +answer.forEach((item) => console.log(item));