Skip to content

Commit 40e3932

Browse files
author
Hamid Gasmi
committed
Issue 87 (Finding an Exit from a Maze): solved with a Graph DFS exploration
1 parent d7faaa6 commit 40e3932

File tree

13 files changed

+108
-6
lines changed

13 files changed

+108
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,63 @@
1-
#Uses python3
2-
31
import sys
2+
from queue import Queue
3+
4+
class MazeGraph:
5+
def __init__(self, n, adj):
6+
self.adj = adj
7+
self.visited = [False] * n
8+
9+
def visit(self, v):
10+
self.visited[v] = True
11+
12+
def isVisited(self, v):
13+
return self.visited[v]
14+
15+
# Time Complexity: O(|V|)
16+
# Space Complexity: O(|V|)
17+
# Good job! (Max time used: 0.03/5.00, max memory used: 9535488/536870912.)
18+
def explore_dfs(self, u, v):
19+
if u == v:
20+
return 1
21+
22+
self.visit(u)
23+
for i in range(len(adj[u])):
24+
if not self.isVisited(adj[u][i]):
25+
if self.explore_dfs(adj[u][i], v) == 1:
26+
return 1
427

5-
def reach(adj, x, y):
6-
#write your code here
7-
return 0
28+
return 0
29+
30+
# Time Complexity: O(|V|)
31+
# Space Complexity: O(|V|)
32+
def explore_bfs(self, u, v, q):
33+
if u == v:
34+
return 1
35+
36+
q.put(u)
37+
while not q.empty():
38+
u = q.get()
39+
if u == v:
40+
return 1
41+
self.visit(u)
42+
43+
for i in range(len(adj[u])):
44+
if not self.isVisited(adj[u][i]):
45+
q.put(adj[u][i])
46+
47+
return 0
48+
49+
def reach(adj, n, u, v, solution):
50+
51+
aMaze = MazeGraph(n, adj)
52+
if solution == 1:
53+
return aMaze.explore_dfs(u, v)
54+
elif solution == 2:
55+
q = Queue(maxsize = n)
56+
return aMaze.explore_bfs(u, v, q)
57+
elif solution == 3:
58+
return 0
59+
else:
60+
return 0
861

962
if __name__ == '__main__':
1063
input = sys.stdin.read()
@@ -18,4 +71,5 @@ def reach(adj, x, y):
1871
for (a, b) in edges:
1972
adj[a - 1].append(b - 1)
2073
adj[b - 1].append(a - 1)
21-
print(reach(adj, x, y))
74+
75+
print(reach(adj, n, x, y, 2))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
4 4
2+
1 2
3+
3 2
4+
4 3
5+
1 4
6+
1 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
4 2
2+
1 2
3+
3 2
4+
1 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
14 13
2+
1 2
3+
1 3
4+
3 5
5+
3 4
6+
4 6
7+
4 7
8+
4 8
9+
6 8
10+
6 9
11+
6 10
12+
8 11
13+
12 13
14+
12 14
15+
1 11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
14 13
2+
1 2
3+
1 3
4+
3 5
5+
3 4
6+
4 6
7+
4 7
8+
4 8
9+
6 8
10+
6 9
11+
6 10
12+
8 11
13+
12 13
14+
12 14
15+
1 14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

0 commit comments

Comments
 (0)