Skip to content

Commit

Permalink
Shortest Bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
BangDori committed Dec 14, 2024
1 parent c2491e0 commit 1a9867e
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions bangdori/Shortest Bridge.js
Original file line number Diff line number Diff line change
@@ -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
};

0 comments on commit 1a9867e

Please sign in to comment.