Skip to content

Commit 37e5f4f

Browse files
committed
1926. Nearest Exit from Entrance in Maze: RETRY
1 parent 1c8abbc commit 37e5f4f

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1451,3 +1451,4 @@ mod s1921_eliminate_maximum_number_of_monsters;
14511451
mod s1922_count_good_numbers;
14521452
mod s1923_longest_common_subpath;
14531453
mod s1925_count_square_sum_triples;
1454+
mod s1926_nearest_exit_from_entrance_in_maze;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* [1926] Nearest Exit from Entrance in Maze
3+
*
4+
* 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.
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+
* Return the number of steps in the shortest path from the entrance to the nearest exit, or -1 if no such path exists.
7+
*
8+
* Example 1:
9+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/nearest1-grid.jpg" style="width: 333px; height: 253px;" />
10+
* Input: maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2]
11+
* Output: 1
12+
* Explanation: There are 3 exits in this maze at [1,0], [0,2], and [2,3].
13+
* Initially, you are at the entrance cell [1,2].
14+
* - You can reach [1,0] by moving 2 steps left.
15+
* - You can reach [0,2] by moving 1 step up.
16+
* It is impossible to reach [2,3] from the entrance.
17+
* Thus, the nearest exit is [0,2], which is 1 step away.
18+
*
19+
* Example 2:
20+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/nearesr2-grid.jpg" style="width: 253px; height: 253px;" />
21+
* Input: maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0]
22+
* Output: 2
23+
* Explanation: There is 1 exit in this maze at [1,2].
24+
* [1,0] does not count as an exit since it is the entrance cell.
25+
* Initially, you are at the entrance cell [1,0].
26+
* - You can reach [1,2] by moving 2 steps right.
27+
* Thus, the nearest exit is [1,2], which is 2 steps away.
28+
*
29+
* Example 3:
30+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/nearest3-grid.jpg" style="width: 173px; height: 93px;" />
31+
* Input: maze = [[".","+"]], entrance = [0,0]
32+
* Output: -1
33+
* Explanation: There are no exits in this maze.
34+
*
35+
*
36+
* Constraints:
37+
*
38+
* maze.length == m
39+
* maze[i].length == n
40+
* 1 <= m, n <= 100
41+
* maze[i][j] is either '.' or '+'.
42+
* entrance.length == 2
43+
* 0 <= entrancerow < m
44+
* 0 <= entrancecol < n
45+
* entrance will always be an empty cell.
46+
*
47+
*/
48+
pub struct Solution {}
49+
50+
// problem: https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/
51+
// discuss: https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/discuss/?currentPage=1&orderBy=most_votes&query=
52+
53+
// submission codes start here
54+
55+
impl Solution {
56+
pub fn nearest_exit(maze: Vec<Vec<char>>, entrance: Vec<i32>) -> i32 {
57+
0
58+
}
59+
}
60+
61+
// submission codes end
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
#[ignore]
69+
fn test_1926_example_1() {
70+
let maze = vec![
71+
vec!['+', '+', '.', '+'],
72+
vec!['.', '.', '.', '+'],
73+
vec!['+', '+', '+', '.'],
74+
];
75+
let entrance = vec![1, 2];
76+
77+
let result = 1;
78+
79+
assert_eq!(Solution::nearest_exit(maze, entrance), result);
80+
}
81+
82+
#[test]
83+
#[ignore]
84+
fn test_1926_example_2() {
85+
let maze = vec![vec!['+', '+', '+'], vec!['.', '.', '.'], ['+', '+', '+']];
86+
let entrance = vec![1, 0];
87+
88+
let result = 2;
89+
90+
assert_eq!(Solution::nearest_exit(maze, entrance), result);
91+
}
92+
93+
#[test]
94+
#[ignore]
95+
fn test_1926_example_3() {
96+
let maze = vec![vec!['.'], vec!['+']];
97+
let entrance = vec![0, 0];
98+
99+
let result = -1;
100+
101+
assert_eq!(Solution::nearest_exit(maze, entrance), result);
102+
}
103+
}

0 commit comments

Comments
 (0)