Skip to content

Commit 4c2d2fc

Browse files
authored
feat: add ts solution to lc problem: No.1926 (#2994)
1 parent 8e11cab commit 4c2d2fc

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@ func nearestExit(maze [][]byte, entrance []int) int {
202202
}
203203
```
204204

205+
#### TypeScript
206+
207+
```ts
208+
function nearestExit(maze: string[][], entrance: number[]): number {
209+
const m = maze.length;
210+
const n = maze[0].length;
211+
const dir = [0, 1, 0, -1, 0];
212+
const q = [[...entrance, 0]];
213+
maze[entrance[0]][entrance[1]] = '+';
214+
for (const [i, j, ans] of q) {
215+
for (let d = 0; d < 4; d++) {
216+
const [x, y] = [i + dir[d], j + dir[d + 1]];
217+
const v = maze[x]?.[y];
218+
if (!v && ans) {
219+
return ans;
220+
}
221+
if (v === '.') {
222+
q.push([x, y, ans + 1]);
223+
maze[x][y] = '+';
224+
}
225+
}
226+
}
227+
return -1;
228+
}
229+
```
230+
205231
<!-- tabs:end -->
206232

207233
<!-- solution:end -->

solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,32 @@ func nearestExit(maze [][]byte, entrance []int) int {
203203
}
204204
```
205205

206+
#### TypeScript
207+
208+
```ts
209+
function nearestExit(maze: string[][], entrance: number[]): number {
210+
const m = maze.length;
211+
const n = maze[0].length;
212+
const dir = [0, 1, 0, -1, 0];
213+
const q = [[...entrance, 0]];
214+
maze[entrance[0]][entrance[1]] = '+';
215+
for (const [i, j, ans] of q) {
216+
for (let d = 0; d < 4; d++) {
217+
const [x, y] = [i + dir[d], j + dir[d + 1]];
218+
const v = maze[x]?.[y];
219+
if (!v && ans) {
220+
return ans;
221+
}
222+
if (v === '.') {
223+
q.push([x, y, ans + 1]);
224+
maze[x][y] = '+';
225+
}
226+
}
227+
}
228+
return -1;
229+
}
230+
```
231+
206232
<!-- tabs:end -->
207233

208234
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function nearestExit(maze: string[][], entrance: number[]): number {
2+
const m = maze.length;
3+
const n = maze[0].length;
4+
const dir = [0, 1, 0, -1, 0];
5+
const q = [[...entrance, 0]];
6+
maze[entrance[0]][entrance[1]] = '+';
7+
for (const [i, j, ans] of q) {
8+
for (let d = 0; d < 4; d++) {
9+
const [x, y] = [i + dir[d], j + dir[d + 1]];
10+
const v = maze[x]?.[y];
11+
if (!v && ans) {
12+
return ans;
13+
}
14+
if (v === '.') {
15+
q.push([x, y, ans + 1]);
16+
maze[x][y] = '+';
17+
}
18+
}
19+
}
20+
return -1;
21+
}

0 commit comments

Comments
 (0)