Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit dfa1bf6

Browse files
committed
added problem47
1 parent c6e97d6 commit dfa1bf6

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

problem47/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 1926. Nearest Exit from Entrance in Maze
2+
3+
You are given an m x n matrix maze (0-indexed) with empty cells (represented as '.') and walls (represented as '+'). You are also given the entrance of the maze, where entrance = [entrancerow, entrancecol] denotes the row and column of the cell you are initially standing at.
4+
5+
In one step, you can move one cell up, down, left, or right. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the nearest exit from the entrance. An exit is defined as an empty cell that is at the border of the maze. The entrance does not count as an exit.
6+
7+
Return the number of steps in the shortest path from the entrance to the nearest exit, or -1 if no such path exists.
8+
9+
10+
## Example 1:
11+
12+
Input: maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2]
13+
Output: 1
14+
Explanation: There are 3 exits in this maze at [1,0], [0,2], and [2,3].
15+
Initially, you are at the entrance cell [1,2].
16+
- You can reach [1,0] by moving 2 steps left.
17+
- You can reach [0,2] by moving 1 step up.
18+
It is impossible to reach [2,3] from the entrance.
19+
Thus, the nearest exit is [0,2], which is 1 step away.
20+
21+
## Example 2:
22+
23+
Input: maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0]
24+
Output: 2
25+
Explanation: There is 1 exit in this maze at [1,2].
26+
[1,0] does not count as an exit since it is the entrance cell.
27+
Initially, you are at the entrance cell [1,0].
28+
- You can reach [1,2] by moving 2 steps right.
29+
Thus, the nearest exit is [1,2], which is 2 steps away.
30+
31+
## Example 3:
32+
33+
Input: maze = [[".","+"]], entrance = [0,0]
34+
Output: -1
35+
Explanation: There are no exits in this maze.
36+
37+
38+
## Constraints:
39+
40+
maze.length == m
41+
maze[i].length == n
42+
1 <= m, n <= 100
43+
maze[i][j] is either '.' or '+'.
44+
entrance.length == 2
45+
0 <= entrancerow < m
46+
0 <= entrancecol < n
47+
entrance will always be an empty cell.

problem47/Solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
3+
queue, steps = [], 0
4+
m, n = len(maze), len(maze[0])
5+
directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
6+
7+
queue.append(entrance)
8+
maze[entrance[0]][entrance[1]] = '+' # create wall to mark visited
9+
10+
# bfs
11+
while (queue):
12+
N = len(queue)
13+
while N:
14+
coordinates = queue.pop(0)
15+
i, j = coordinates[0], coordinates[1]
16+
# exit found if at a boundary which is not the entrance
17+
if (i == 0 or i == m-1 or j == 0 or j == n-1) and coordinates != entrance:
18+
return steps
19+
# explore 4 directions
20+
for dir in directions:
21+
# create new coordinates
22+
new_i = i + dir[0]
23+
new_j = j + dir[1]
24+
# check new coordinates are within boundary and there is no wall
25+
if new_i >= 0 and new_i < m and new_j >= 0 and new_j < n and maze[new_i][new_j] == '.':
26+
queue.append([new_i, new_j])
27+
maze[new_i][new_j] = '+' # create wall to mark visited
28+
N -= 1
29+
steps += 1
30+
return -1

0 commit comments

Comments
 (0)