Skip to content

Commit

Permalink
208th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 22, 2024
1 parent 828b5cd commit 5eafcdf
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
| ------------------------------------- | -------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 5eafcdf

Please sign in to comment.