From 1a9867ec1428a9c17774c8211084f0a3d347f588 Mon Sep 17 00:00:00 2001 From: BangDori Date: Sun, 15 Dec 2024 00:44:53 +0900 Subject: [PATCH 1/2] Shortest Bridge --- bangdori/Shortest Bridge.js | 94 +++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 bangdori/Shortest Bridge.js diff --git a/bangdori/Shortest Bridge.js b/bangdori/Shortest Bridge.js new file mode 100644 index 0000000..62f622a --- /dev/null +++ b/bangdori/Shortest Bridge.js @@ -0,0 +1,94 @@ +const WATER = 0; +const ISLAND = 1; +const ISLAND2 = 2; +const DIRS = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], +]; + +/** + * @param {number[][]} grid + * @return {number} + */ +var shortestBridge = function (grid) { + const rows = grid.length; + const cols = grid[0].length; + + let queue = []; + + const exploreIslandDFS = (row, col) => { + if (grid[row][col] === ISLAND2) return; + + grid[row][col] = ISLAND2; + queue.push([row, col]); + + for (let [dx, dy] of DIRS) { + const nextRow = row + dx; + const nextCol = col + dy; + + if ( + nextRow >= 0 && + nextRow < rows && + nextCol >= 0 && + nextCol < cols && + grid[nextRow][nextCol] === ISLAND + ) { + exploreIslandDFS(nextRow, nextCol); + } + } + }; + + const connectIslandBFS = () => { + /** + * 이동 거리를 나타내는 변수 + * ISLAND와 차이를 두기 위해 음수값으로 적용 + */ + let distance = -1; + let currentQueue = queue; + + while (currentQueue.length > 0) { + queue = []; + + for (const [row, col] of currentQueue) { + for (const [dx, dy] of DIRS) { + const nextRow = row + dx; + const nextCol = col + dy; + + if ( + nextRow >= 0 && + nextRow < rows && + nextCol >= 0 && + nextCol < cols + ) { + if (grid[nextRow][nextCol] === WATER) { + queue.push([nextRow, nextCol]); + grid[nextRow][nextCol] = distance; + } + + if (grid[nextRow][nextCol] === ISLAND) { + return (distance + 1) * -1; + } + } + } + } + + distance -= 1; + currentQueue = queue; + } + + return 1; + }; + + for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + if (grid[r][c] === WATER) continue; + + exploreIslandDFS(r, c); + return connectIslandBFS(); + } + } + + return -1; // Error +}; From e7cdea32846293d5baf47b3edcf8baaab3ee83d6 Mon Sep 17 00:00:00 2001 From: BangDori Date: Mon, 16 Dec 2024 00:22:53 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bangdori/Shortest Bridge.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/bangdori/Shortest Bridge.js b/bangdori/Shortest Bridge.js index 62f622a..49e4cde 100644 --- a/bangdori/Shortest Bridge.js +++ b/bangdori/Shortest Bridge.js @@ -29,10 +29,7 @@ var shortestBridge = function (grid) { const nextCol = col + dy; if ( - nextRow >= 0 && - nextRow < rows && - nextCol >= 0 && - nextCol < cols && + isValidCoords(nextRow, nextCol) && grid[nextRow][nextCol] === ISLAND ) { exploreIslandDFS(nextRow, nextCol); @@ -56,12 +53,7 @@ var shortestBridge = function (grid) { const nextRow = row + dx; const nextCol = col + dy; - if ( - nextRow >= 0 && - nextRow < rows && - nextCol >= 0 && - nextCol < cols - ) { + if (isValidCoords(nextRow, nextCol)) { if (grid[nextRow][nextCol] === WATER) { queue.push([nextRow, nextCol]); grid[nextRow][nextCol] = distance; @@ -81,6 +73,9 @@ var shortestBridge = function (grid) { return 1; }; + const isValidCoords = (row, col) => + row >= 0 && row < rows && col >= 0 && col < cols; + for (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { if (grid[r][c] === WATER) continue;