-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
828b5cd
commit 5eafcdf
Showing
3 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
src/page-18/1926. Nearest Exit from Entrance in Maze/nearestExit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |