-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathbinaryMaze.py
More file actions
103 lines (81 loc) · 2.4 KB
/
binaryMaze.py
File metadata and controls
103 lines (81 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from collections import deque
ROW = 9
COL = 10
# To store matrix cell coordinates
class Point:
def __init__(self,x: int, y: int):
self.x = x
self.y = y
# A data structure for queue used in BFS
class queueNode:
def __init__(self,pt: Point, dist: int):
self.pt = pt # The coordinates of the cell
self.dist = dist # Cell's distance from the source
# Check whether given cell(row,col)
# is a valid cell or not
def isValid(row: int, col: int):
return (row >= 0) and (row < ROW) and
(col >= 0) and (col < COL)
# These arrays are used to get row and column
# numbers of 4 neighbours of a given cell
rowNum = [-1, 0, 0, 1]
colNum = [0, -1, 1, 0]
# Function to find the shortest path between
# a given source cell to a destination cell.
def BFS(mat, src: Point, dest: Point):
# check source and destination cell
# of the matrix have value 1
if mat[src.x][src.y]!=1 or mat[dest.x][dest.y]!=1:
return -1
visited = [[False for i in range(COL)]
for j in range(ROW)]
# Mark the source cell as visited
visited[src.x][src.y] = True
# Create a queue for BFS
q = deque()
# Distance of source cell is 0
s = queueNode(src,0)
q.append(s) # Enqueue source cell
# Do a BFS starting from source cell
while q:
curr = q.popleft() # Dequeue the front cell
# If we have reached the destination cell,
# we are done
pt = curr.pt
if pt.x == dest.x and pt.y == dest.y:
return curr.dist
# Otherwise enqueue its adjacent cells
for i in range(4):
row = pt.x + rowNum[i]
col = pt.y + colNum[i]
# if adjacent cell is valid, has path
# and not visited yet, enqueue it.
if (isValid(row,col) and
mat[row][col] == 1 and
not visited[row][col]):
visited[row][col] = True
Adjcell = queueNode(Point(row,col),
curr.dist+1)
q.append(Adjcell)
# Return -1 if destination cannot be reached
return -1
# Driver code
def main():
mat = [[ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 ],
[ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 ],
[ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 ],
[ 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 ],
[ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 ],
[ 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 ]]
source = Point(0,0)
dest = Point(3,4)
dist = BFS(mat,source,dest)
if dist!=-1:
print("Shortest Path is",dist)
else:
print("Shortest Path doesn't exist")
main()
# This code is contributed by stutipathak31jan