From 56708849f8a6acf05c18e0a964dbcc274809abcf Mon Sep 17 00:00:00 2001 From: devwqc Date: Sun, 8 Sep 2024 17:50:05 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[=EC=A0=95=EB=B4=89=EC=B0=AC]=209-7.=20?= =?UTF-8?q?=EC=84=AC=EB=82=98=EB=9D=BC=20=EC=95=84=EC=9D=BC=EB=9E=9C?= =?UTF-8?q?=EB=93=9C(BFS)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git "a/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" "b/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" index 4401b460..c1afbbc5 100644 --- "a/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" +++ "b/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" @@ -1,6 +1,61 @@ +/* +✅문제 제목: 섬나라 아일랜드(BFS) + +✅문제 유형: BFS + +✅문제 풀이 날짜: 2024-09-08 + +💡문제 분석 요약 + - N * N 격자판이 주어진다. + - 각 섬은 1로 표시되어 상하좌우와 대각선으로 연결되어 있다. 0은 바다이다. + - 몇 개의 섬이 있는지 구하라. + - N(3<=N<=20) + +💡알고리즘 설계 + - 방문할 노드를 담을 queue를 빈 배열로 선언한다. + - 이동을 제어할 dx, dy를 12시, 1.5시, 3시, 4.5시, 6시, 7.5시, 9시, 10.5시 방향을 기준으로 배열을 선언한다. + - board의 길이 만큼 중첩 반복문을 돈다. + - 방문할 수 없으면 continue한다. + - 그 외에는 queue에 [y, x]의 좌표를 push하고 answer를 1더한다. + - queue의 길이만큼 while문을 돈다. + - queue에서 [y, x] 좌표를 꺼내고 방문처리한다. + - dx의 길이만큼 반복문 돌면서 방문 가능한 섬을 queue에 push한다. +*/ + function solution(board) { let answer = 0; + const N = board.length; + const queue = []; + const dx = [0, 1, 1, 1, 0, -1, -1, -1]; + const dy = [-1, -1, 0, 1, 1, 1, 0, -1]; + + for (let i = 0; i < N; i++) { + for (let j = 0; j < N; j++) { + if (!board[i][j]) { + continue; + } + + queue.push([i, j]); + answer++; + while (queue.length) { + const [y, x] = queue.shift(); + board[y][x] = 0; + + for (let k = 0; k < dx.length; k++) { + const nx = x + dx[k]; + const ny = y + dy[k]; + + if (nx < 0 || nx >= N || ny < 0 || ny >= N || !board[ny][nx]) { + continue; + } + + queue.push([ny, nx]); + } + } + } + } + return answer; } @@ -14,4 +69,4 @@ let arr = [ [1, 0, 1, 0, 1, 0, 0], ]; -console.log(solution(arr)); +console.log(solution(arr)); // 5 From 01a8111f5d92756cf8e4c660308847a7af15ee2d Mon Sep 17 00:00:00 2001 From: devwqc Date: Sun, 8 Sep 2024 18:10:26 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[=EC=A0=95=EB=B4=89=EC=B0=AC]=209-7.=20?= =?UTF-8?q?=EC=84=AC=EB=82=98=EB=9D=BC=20=EC=95=84=EC=9D=BC=EB=9E=9C?= =?UTF-8?q?=EB=93=9C(BFS)=20=EB=B0=A9=EB=AC=B8=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\240\225\353\264\211\354\260\254/index.js" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" "b/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" index c1afbbc5..c04eeedc 100644 --- "a/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" +++ "b/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" @@ -36,11 +36,11 @@ function solution(board) { continue; } + board[i][j] = 0; queue.push([i, j]); answer++; while (queue.length) { const [y, x] = queue.shift(); - board[y][x] = 0; for (let k = 0; k < dx.length; k++) { const nx = x + dx[k]; @@ -50,6 +50,7 @@ function solution(board) { continue; } + board[ny][nx] = 0; queue.push([ny, nx]); } } From 21e09570b8c0d7cb1539e70ef63128d4c5494fcd Mon Sep 17 00:00:00 2001 From: devwqc Date: Sun, 8 Sep 2024 18:12:37 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[=EC=A0=95=EB=B4=89=EC=B0=AC]=209-7.=20?= =?UTF-8?q?=EC=84=AC=EB=82=98=EB=9D=BC=20=EC=95=84=EC=9D=BC=EB=9E=9C?= =?UTF-8?q?=EB=93=9C(BFS)=20=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98=20?= =?UTF-8?q?=EC=84=A4=EA=B3=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\240\225\353\264\211\354\260\254/index.js" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" "b/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" index c04eeedc..a97f5181 100644 --- "a/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" +++ "b/09. \352\267\270\353\236\230\355\224\204\354\231\200 \355\203\220\354\203\211(DFS, BFS;\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211)/07. \354\204\254\353\202\230\353\235\274 \354\225\204\354\235\274\353\236\234\353\223\234(BFS)/\354\240\225\353\264\211\354\260\254/index.js" @@ -18,7 +18,7 @@ - 방문할 수 없으면 continue한다. - 그 외에는 queue에 [y, x]의 좌표를 push하고 answer를 1더한다. - queue의 길이만큼 while문을 돈다. - - queue에서 [y, x] 좌표를 꺼내고 방문처리한다. + - queue에 넣을 때 항상 방문처리한다. - dx의 길이만큼 반복문 돌면서 방문 가능한 섬을 queue에 push한다. */