From dd7ca9f4c2bb2d144831ede9323ea6b502ece2ea Mon Sep 17 00:00:00 2001 From: devwqc Date: Sun, 8 Sep 2024 15:45:13 +0900 Subject: [PATCH 1/2] =?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(DFS)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 61 ++++++++++++++++++- 1 file changed, 60 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(DFS)/\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(DFS)/\354\240\225\353\264\211\354\260\254/index.js" index 4401b460..1258be74 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(DFS)/\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(DFS)/\354\240\225\353\264\211\354\260\254/index.js" @@ -1,6 +1,65 @@ +/* +✅문제 제목: 섬나라 아일랜드(DFS) + +✅문제 유형: DFS + +✅문제 풀이 날짜: 2024-09-08 + +💡문제 분석 요약 + - N*N 격자판이 있다. + - 각 섬은 1로 표시되어 상하좌우와 대각선으로 연결되어 있다. + - 0은 바다이다. + - N(3<=N<=20) + +💡알고리즘 설계 + - 12시, 1.5시, 3시, 4.5시, 6시, 7.5시, 9시, 10.5시를 기준으로 dx, dy 배열을 생성한다. + - 좌표 평면 기준이 아닌 배열의 인덱스를 기준으로 dx, dy를 계산한다. + - 탐색한 섬을 저장할 ch 배열을 선언한다. + - board를 중첩 반복문을 통해서 0, 0부터 N, N까지 재귀함수를 호출한다. + - 이때 바다나 이미 방문한 섬은 continue로 패스한다. + - 그 외에는 answer를 1 더하고 재귀함수를 호출한다. + - 재귀함수에서 인접한 섬을 재귀탐색하면서 탐색 여부를 체크한다. +*/ + function solution(board) { let answer = 0; + const N = board.length; + const dy = [-1, -1, 0, 1, 1, 1, 0, -1]; + const dx = [0, 1, 1, 1, 0, -1, -1, -1]; + const ch = Array.from({ length: N + 1 }, () => Array(N + 1).fill(0)); + + function DFS(y, x) { + for (let i = 0; i < dx.length; i++) { + const ny = y + dy[i]; + const nx = x + dx[i]; + + if ( + nx < 0 || + nx >= N || + ny < 0 || + ny >= N || + ch[ny][nx] || + !board[y][x] + ) { + continue; + } + + ch[ny][nx] = 1; + DFS(ny, nx); + } + } + + for (let i = 0; i < N; i++) { + for (let j = 0; j < N; j++) { + if (!board[i][j] || ch[i][j]) { + continue; + } + answer++; + DFS(i, j); + } + } + return answer; } @@ -14,4 +73,4 @@ let arr = [ [1, 0, 1, 0, 1, 0, 0], ]; -console.log(solution(arr)); +console.log(solution(arr)); // 5 From b6c6e60b2887a951f73082a7d90d12bf85a6ae04 Mon Sep 17 00:00:00 2001 From: devwqc Date: Sun, 8 Sep 2024 16:45:10 +0900 Subject: [PATCH 2/2] =?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(DFS)=20ch=EB=B0=B0=EC=97=B4=20=EC=A0=9C=EA=B1=B0=20?= =?UTF-8?q?=ED=9B=84=20board=EC=9D=98=20=ED=83=90=EC=83=89=20=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=20=EC=97=AC=EB=B6=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) 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(DFS)/\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(DFS)/\354\240\225\353\264\211\354\260\254/index.js" index 1258be74..d80834ed 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(DFS)/\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(DFS)/\354\240\225\353\264\211\354\260\254/index.js" @@ -14,10 +14,10 @@ 💡알고리즘 설계 - 12시, 1.5시, 3시, 4.5시, 6시, 7.5시, 9시, 10.5시를 기준으로 dx, dy 배열을 생성한다. - 좌표 평면 기준이 아닌 배열의 인덱스를 기준으로 dx, dy를 계산한다. - - 탐색한 섬을 저장할 ch 배열을 선언한다. - board를 중첩 반복문을 통해서 0, 0부터 N, N까지 재귀함수를 호출한다. - 이때 바다나 이미 방문한 섬은 continue로 패스한다. - 그 외에는 answer를 1 더하고 재귀함수를 호출한다. + - 방문한 지역을 0으로 변경하여 방문 여부를 표시한다. - 재귀함수에서 인접한 섬을 재귀탐색하면서 탐색 여부를 체크한다. */ @@ -27,32 +27,25 @@ function solution(board) { const N = board.length; const dy = [-1, -1, 0, 1, 1, 1, 0, -1]; const dx = [0, 1, 1, 1, 0, -1, -1, -1]; - const ch = Array.from({ length: N + 1 }, () => Array(N + 1).fill(0)); function DFS(y, x) { + board[y][x] = 0; + for (let i = 0; i < dx.length; i++) { const ny = y + dy[i]; const nx = x + dx[i]; - if ( - nx < 0 || - nx >= N || - ny < 0 || - ny >= N || - ch[ny][nx] || - !board[y][x] - ) { + if (nx < 0 || nx >= N || ny < 0 || ny >= N || !board[ny][nx]) { continue; } - ch[ny][nx] = 1; DFS(ny, nx); } } for (let i = 0; i < N; i++) { for (let j = 0; j < N; j++) { - if (!board[i][j] || ch[i][j]) { + if (!board[i][j]) { continue; } answer++;