Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit334127f

Browse files
add 1926
1 parentcf7aa7b commit334127f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag 1
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1926|[Nearest Exit from Entrance in Maze](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1926.java)||Medium|DP, DFS, BFS|
1112
|1925|[Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1925.java)||Easy|Array, Greedy|
1213
|1920|[Build Array from Permutation](https://leetcode.com/problems/build-array-from-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1920.java)||Easy||
1314
|1913|[Maximum Product Difference Between Two Pairs](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1913.java)||Easy|Sort|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.LinkedList;
4+
importjava.util.Queue;
5+
6+
publicclass_1926 {
7+
publicstaticclassSolution1 {
8+
publicintnearestExit(char[][]maze,int[]entrance) {
9+
intm =maze.length;
10+
intn =maze[0].length;
11+
int[]directions =newint[]{0,1,0, -1,0};
12+
Queue<int[]>queue =newLinkedList<>();
13+
queue.offer(newint[]{entrance[0],entrance[1],0});
14+
boolean[][]visited =newboolean[m][n];
15+
visited[entrance[0]][entrance[1]] =true;
16+
intshortestSteps =m *n;
17+
while (!queue.isEmpty()) {
18+
int[]curr =queue.poll();
19+
for (inti =0;i <directions.length -1;i++) {
20+
intnextX =curr[0] +directions[i];
21+
intnextY =curr[1] +directions[i +1];
22+
if (nextX >=0 &&nextX <m &&nextY >=0 &&nextY <n &&maze[nextX][nextY] =='.' && !visited[nextX][nextY]) {
23+
visited[nextX][nextY] =true;
24+
if (nextX ==0 ||nextX ==m -1 ||nextY ==0 ||nextY ==n -1) {
25+
shortestSteps =Math.min(shortestSteps,curr[2] +1);
26+
}else {
27+
queue.offer(newint[]{nextX,nextY,curr[2] +1});
28+
}
29+
}
30+
}
31+
}
32+
returnshortestSteps ==m *n ? -1 :shortestSteps;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp