@@ -60,18 +60,131 @@ Thus, the nearest exit is [1,2], which is 2 steps away.
60
60
61
61
## Solutions
62
62
63
+ BFS.
64
+
63
65
<!-- tabs:start -->
64
66
65
67
### ** Python3**
66
68
67
69
``` python
68
-
70
+ class Solution :
71
+ def nearestExit (self , maze : List[List[str ]], entrance : List[int ]) -> int :
72
+ m, n = len (maze), len (maze[0 ])
73
+ i, j = entrance
74
+ q = deque([(i, j)])
75
+ maze[i][j] = ' +'
76
+ ans = 0
77
+ while q:
78
+ ans += 1
79
+ for _ in range (len (q), 0 , - 1 ):
80
+ i, j = q.popleft()
81
+ for a, b in [[0 , - 1 ], [0 , 1 ], [- 1 , 0 ], [1 , 0 ]]:
82
+ x, y = i + a, j + b
83
+ if 0 <= x < m and 0 <= y < n and maze[x][y] == ' .' :
84
+ if x == 0 or x == m - 1 or y == 0 or y == n - 1 :
85
+ return ans
86
+ q.append((x, y))
87
+ maze[x][y] = ' +'
88
+ return - 1
69
89
```
70
90
71
91
### ** Java**
72
92
73
93
``` java
94
+ class Solution {
95
+ public int nearestExit (char [][] maze , int [] entrance ) {
96
+ int m = maze. length;
97
+ int n = maze[0 ]. length;
98
+ Deque<int[]> q = new ArrayDeque<> ();
99
+ q. offer(entrance);
100
+ maze[entrance[0 ]][entrance[1 ]] = ' +' ;
101
+ int ans = 0 ;
102
+ int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
103
+ while (! q. isEmpty()) {
104
+ ++ ans;
105
+ for (int k = q. size(); k > 0 ; -- k) {
106
+ int [] p = q. poll();
107
+ int i = p[0 ], j = p[1 ];
108
+ for (int l = 0 ; l < 4 ; ++ l) {
109
+ int x = i + dirs[l], y = j + dirs[l + 1 ];
110
+ if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == ' .' ) {
111
+ if (x == 0 || x == m - 1 || y == 0 || y == n - 1 ) {
112
+ return ans;
113
+ }
114
+ q. offer(new int []{x, y});
115
+ maze[x][y] = ' +' ;
116
+ }
117
+ }
118
+ }
119
+ }
120
+ return - 1 ;
121
+ }
122
+ }
123
+ ```
124
+
125
+ ### ** C++**
126
+
127
+ ``` cpp
128
+ class Solution {
129
+ public:
130
+ int nearestExit(vector<vector<char >>& maze, vector<int >& entrance) {
131
+ int m = maze.size(), n = maze[ 0] .size();
132
+ queue<vector<int >> q{{entrance}};
133
+ maze[ entrance[ 0]] [ entrance[ 1]] = '+';
134
+ int ans = 0;
135
+ vector<int > dirs = {-1, 0, 1, 0, -1};
136
+ while (!q.empty())
137
+ {
138
+ ++ans;
139
+ for (int k = q.size(); k > 0; --k)
140
+ {
141
+ auto p = q.front();
142
+ q.pop();
143
+ for (int l = 0; l < 4; ++l)
144
+ {
145
+ int x = p[ 0] + dirs[ l] , y = p[ 1] + dirs[ l + 1] ;
146
+ if (x >= 0 && x < m && y >= 0 && y < n && maze[ x] [ y ] == '.')
147
+ {
148
+ if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans;
149
+ q.push({x, y});
150
+ maze[ x] [ y ] = '+';
151
+ }
152
+ }
153
+ }
154
+ }
155
+ return -1;
156
+ }
157
+ };
158
+ ```
74
159
160
+ ### **Go**
161
+
162
+ ```go
163
+ func nearestExit(maze [][]byte, entrance []int) int {
164
+ m, n := len(maze), len(maze[0])
165
+ q := [][]int{entrance}
166
+ maze[entrance[0]][entrance[1]] = '+'
167
+ ans := 0
168
+ dirs := []int{-1, 0, 1, 0, -1}
169
+ for len(q) > 0 {
170
+ ans++
171
+ for k := len(q); k > 0; k-- {
172
+ p := q[0]
173
+ q = q[1:]
174
+ for l := 0; l < 4; l++ {
175
+ x, y := p[0]+dirs[l], p[1]+dirs[l+1]
176
+ if x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' {
177
+ if x == 0 || x == m-1 || y == 0 || y == n-1 {
178
+ return ans
179
+ }
180
+ q = append(q, []int{x, y})
181
+ maze[x][y] = '+'
182
+ }
183
+ }
184
+ }
185
+ }
186
+ return -1
187
+ }
75
188
```
76
189
77
190
### ** ...**
0 commit comments