Skip to content

Commit 50bf687

Browse files
committedFeb 15, 2021
feat: add python and java solutions to leetcode problem: No.0490
1 parent 6b83385 commit 50bf687

File tree

4 files changed

+184
-2
lines changed

4 files changed

+184
-2
lines changed
 

‎solution/0400-0499/0490.The Maze/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,84 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
深度优先搜索或广度优先搜索实现。
70+
6971
<!-- tabs:start -->
7072

7173
### **Python3**
7274

7375
<!-- 这里可写当前语言的特殊实现逻辑 -->
7476

75-
```python
77+
深度优先搜索。
7678

79+
```python
80+
class Solution:
81+
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
82+
def dfs(maze, start, destination):
83+
if visited[start[0]][start[1]]:
84+
return False
85+
if start[0] == destination[0] and start[1] == destination[1]:
86+
return True
87+
visited[start[0]][start[1]] = True
88+
l, r, u, d = start[1] - 1, start[1] + 1, start[0] - 1, start[0] + 1
89+
while l >= 0 and maze[start[0]][l] == 0:
90+
l -= 1
91+
if dfs(maze, [start[0], l + 1], destination):
92+
return True
93+
while r < len(maze[0]) and maze[start[0]][r] == 0:
94+
r += 1
95+
if dfs(maze, [start[0], r - 1], destination):
96+
return True
97+
while u >= 0 and maze[u][start[1]] == 0:
98+
u -= 1
99+
if dfs(maze, [u + 1, start[1]], destination):
100+
return True
101+
while d < len(maze) and maze[d][start[1]] == 0:
102+
d += 1
103+
if dfs(maze, [d - 1, start[1]], destination):
104+
return True
105+
return False
106+
107+
visited = [[False for _ in maze[0]] for _ in maze]
108+
return dfs(maze, start, destination)
77109
```
78110

79111
### **Java**
80112

81113
<!-- 这里可写当前语言的特殊实现逻辑 -->
82114

83115
```java
116+
class Solution {
117+
private boolean[][] visited;
118+
119+
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
120+
int m = maze.length, n = maze[0].length;
121+
visited = new boolean[m][n];
122+
return dfs(maze, start, destination);
123+
}
124+
125+
private boolean dfs(int[][] maze, int[] start, int[] destination) {
126+
if (visited[start[0]][start[1]]) return false;
127+
if (start[0] == destination[0] && start[1] == destination[1]) return true;
128+
visited[start[0]][start[1]] = true;
129+
130+
int l = start[1] - 1, r = start[1] + 1, u = start[0] - 1, d = start[0] + 1;
131+
132+
while (l >= 0 && maze[start[0]][l] == 0) --l;
133+
if (dfs(maze, new int[]{start[0], l + 1}, destination)) return true;
134+
135+
while (r < maze[0].length && maze[start[0]][r] == 0) ++r;
136+
if (dfs(maze, new int[]{start[0], r - 1}, destination)) return true;
137+
138+
while (u >= 0 && maze[u][start[1]] == 0) --u;
139+
if (dfs(maze, new int[]{u + 1, start[1]}, destination)) return true;
140+
141+
while (d < maze.length && maze[d][start[1]] == 0) ++d;
142+
if (dfs(maze, new int[]{d - 1, start[1]}, destination)) return true;
84143

144+
return false;
145+
}
146+
}
85147
```
86148

87149
### **...**

‎solution/0400-0499/0490.The Maze/README_EN.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,74 @@
6969

7070
### **Python3**
7171

72-
```python
72+
DFS.
7373

74+
```python
75+
class Solution:
76+
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
77+
def dfs(maze, start, destination):
78+
if visited[start[0]][start[1]]:
79+
return False
80+
if start[0] == destination[0] and start[1] == destination[1]:
81+
return True
82+
visited[start[0]][start[1]] = True
83+
l, r, u, d = start[1] - 1, start[1] + 1, start[0] - 1, start[0] + 1
84+
while l >= 0 and maze[start[0]][l] == 0:
85+
l -= 1
86+
if dfs(maze, [start[0], l + 1], destination):
87+
return True
88+
while r < len(maze[0]) and maze[start[0]][r] == 0:
89+
r += 1
90+
if dfs(maze, [start[0], r - 1], destination):
91+
return True
92+
while u >= 0 and maze[u][start[1]] == 0:
93+
u -= 1
94+
if dfs(maze, [u + 1, start[1]], destination):
95+
return True
96+
while d < len(maze) and maze[d][start[1]] == 0:
97+
d += 1
98+
if dfs(maze, [d - 1, start[1]], destination):
99+
return True
100+
return False
101+
102+
visited = [[False for _ in maze[0]] for _ in maze]
103+
return dfs(maze, start, destination)
74104
```
75105

76106
### **Java**
77107

78108
```java
109+
class Solution {
110+
private boolean[][] visited;
111+
112+
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
113+
int m = maze.length, n = maze[0].length;
114+
visited = new boolean[m][n];
115+
return dfs(maze, start, destination);
116+
}
117+
118+
private boolean dfs(int[][] maze, int[] start, int[] destination) {
119+
if (visited[start[0]][start[1]]) return false;
120+
if (start[0] == destination[0] && start[1] == destination[1]) return true;
121+
visited[start[0]][start[1]] = true;
122+
123+
int l = start[1] - 1, r = start[1] + 1, u = start[0] - 1, d = start[0] + 1;
124+
125+
while (l >= 0 && maze[start[0]][l] == 0) --l;
126+
if (dfs(maze, new int[]{start[0], l + 1}, destination)) return true;
127+
128+
while (r < maze[0].length && maze[start[0]][r] == 0) ++r;
129+
if (dfs(maze, new int[]{start[0], r - 1}, destination)) return true;
130+
131+
while (u >= 0 && maze[u][start[1]] == 0) --u;
132+
if (dfs(maze, new int[]{u + 1, start[1]}, destination)) return true;
133+
134+
while (d < maze.length && maze[d][start[1]] == 0) ++d;
135+
if (dfs(maze, new int[]{d - 1, start[1]}, destination)) return true;
79136

137+
return false;
138+
}
139+
}
80140
```
81141

82142
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
private boolean[][] visited;
3+
4+
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
5+
int m = maze.length, n = maze[0].length;
6+
visited = new boolean[m][n];
7+
return dfs(maze, start, destination);
8+
}
9+
10+
private boolean dfs(int[][] maze, int[] start, int[] destination) {
11+
if (visited[start[0]][start[1]]) return false;
12+
if (start[0] == destination[0] && start[1] == destination[1]) return true;
13+
visited[start[0]][start[1]] = true;
14+
15+
int l = start[1] - 1, r = start[1] + 1, u = start[0] - 1, d = start[0] + 1;
16+
17+
while (l >= 0 && maze[start[0]][l] == 0) --l;
18+
if (dfs(maze, new int[]{start[0], l + 1}, destination)) return true;
19+
20+
while (r < maze[0].length && maze[start[0]][r] == 0) ++r;
21+
if (dfs(maze, new int[]{start[0], r - 1}, destination)) return true;
22+
23+
while (u >= 0 && maze[u][start[1]] == 0) --u;
24+
if (dfs(maze, new int[]{u + 1, start[1]}, destination)) return true;
25+
26+
while (d < maze.length && maze[d][start[1]] == 0) ++d;
27+
if (dfs(maze, new int[]{d - 1, start[1]}, destination)) return true;
28+
29+
return false;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
3+
def dfs(maze, start, destination):
4+
if visited[start[0]][start[1]]:
5+
return False
6+
if start[0] == destination[0] and start[1] == destination[1]:
7+
return True
8+
visited[start[0]][start[1]] = True
9+
l, r, u, d = start[1] - 1, start[1] + 1, start[0] - 1, start[0] + 1
10+
while l >= 0 and maze[start[0]][l] == 0:
11+
l -= 1
12+
if dfs(maze, [start[0], l + 1], destination):
13+
return True
14+
while r < len(maze[0]) and maze[start[0]][r] == 0:
15+
r += 1
16+
if dfs(maze, [start[0], r - 1], destination):
17+
return True
18+
while u >= 0 and maze[u][start[1]] == 0:
19+
u -= 1
20+
if dfs(maze, [u + 1, start[1]], destination):
21+
return True
22+
while d < len(maze) and maze[d][start[1]] == 0:
23+
d += 1
24+
if dfs(maze, [d - 1, start[1]], destination):
25+
return True
26+
return False
27+
28+
visited = [[False for _ in maze[0]] for _ in maze]
29+
return dfs(maze, start, destination)

0 commit comments

Comments
 (0)
Please sign in to comment.