diff --git a/README.md b/README.md index dd0fcff..32a27da 100644 --- a/README.md +++ b/README.md @@ -213,10 +213,12 @@ Ace Coding Interview with 75 Qs [1466]: ./src/page-14/1466.%20Reorder%20Routes%20to%20Make%20All%20Paths%20Lead%20to%20the%20City%20Zero/minReorder.ts [399]: ./src/page-4/399.%20Evaluate%20Division/calcEquation.ts -| Graphs - BFS | | | -| ---------------------------------------- | -------- | ------ | -| 1926. Nearest Exit from Entrance in Maze | Solution | Medium | -| 994. Rotting Oranges | Solution | Medium | +| Graphs - BFS | | | +| ---------------------------------------- | ---------------- | ------ | +| 1926. Nearest Exit from Entrance in Maze | [Solution][1926] | Medium | +| 994. Rotting Oranges | Solution | Medium | + +[1926]: ./src/page-18/1926.%20Nearest%20Exit%20from%20Entrance%20in%20Maze/nearestExit.ts | Heap / Priority Queue | | | | ------------------------------------- | -------- | ------ | diff --git a/src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.test.ts b/src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.test.ts new file mode 100644 index 0000000..d16fa8e --- /dev/null +++ b/src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.test.ts @@ -0,0 +1,29 @@ +import { nearestExit } from './nearestExit'; + +describe('1926. Nearest Exit from Entrance in Maze', () => { + test('nearestExit', () => { + expect( + nearestExit( + [ + ['+', '+', '.', '+'], + ['.', '.', '.', '+'], + ['+', '+', '+', '.'], + ], + [1, 2], + ), + ).toBe(1); + + expect( + nearestExit( + [ + ['+', '+', '+'], + ['.', '.', '.'], + ['+', '+', '+'], + ], + [1, 0], + ), + ).toBe(2); + + expect(nearestExit([['.', '+']], [0, 0])).toBe(-1); + }); +}); diff --git a/src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.ts b/src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.ts new file mode 100644 index 0000000..6745a31 --- /dev/null +++ b/src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.ts @@ -0,0 +1,63 @@ +type NearestExit = (maze: string[][], entrance: number[]) => number; + +/** + * Accepted + */ +export const nearestExit: NearestExit = (maze, entrance) => { + const rows = maze.length; + const cols = maze[0].length; + + // Directions for moving up, down, left, and right. + const directions = [ + [-1, 0], // Up + [1, 0], // Down + [0, -1], // Left + [0, 1], // Right + ]; + + const queue: [number, number, number][] = []; // [row, col, steps] + const [startRow, startCol] = entrance; + + // Start BFS from the entrance + queue.push([startRow, startCol, 0]); + maze[startRow][startCol] = '+'; // Mark the entrance as visited by converting it to a wall. + + while (queue.length > 0) { + const current = queue.shift(); + if (current === undefined) continue; // Safely handle empty queue + + const [row, col, steps] = current; + + // Check all four possible directions. + for (const [dRow, dCol] of directions) { + const newRow = row + dRow; + const newCol = col + dCol; + + // If the new position is out of bounds or is a wall, skip it. + if ( + newRow < 0 || + newRow >= rows || + newCol < 0 || + newCol >= cols || + maze[newRow][newCol] === '+' + ) { + continue; + } + + // Check if the new position is an exit (on the border) and not the entrance. + if ( + (newRow === 0 || newRow === rows - 1 || newCol === 0 || newCol === cols - 1) && + (newRow !== startRow || newCol !== startCol) + ) { + return steps + 1; // We found the nearest exit. + } + + // Mark the cell as visited and add it to the queue. + maze[newRow][newCol] = '+'; + queue.push([newRow, newCol, steps + 1]); + } + } + + // No exit found. + return -1; +};