-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.py
153 lines (127 loc) · 4.14 KB
/
bfs.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import mazeGenerator
class Queue:
def __init__(self):
self.list = []
self.front = 0
self.last = -1
def add(self, node):
self.list.append(node)
self.last = self.last + 1
def remove(self):
if self.front > self.last:
raise Exception("Underflow!")
else:
self.front = self.front + 1
return self.list[self.front-1]
def isEmpty(self):
if self.front > self.last:
return True
else:
return False
class Coordinate:
def __init__(self, x, y):
self.x = x
self.y = y
class Node:
def __init__(self, coordinates, previous):
self.state = coordinates
self.previous = previous
class Maze:
matrix = []
visited = []
row = 0
column = 0
start = Coordinate(-1, -1)
goal = Coordinate(-1, -1)
def __init__(self, matrix, row, column):
self.matrix = matrix
self.visited = [['0'] * column for _ in range(row)]
self.row = row
self.column = column
def load(self):
for i in range(self.row):
for j in range(self.column):
if self.matrix[i][j] == 'S':
self.start.x = j
self.start.y = i
if self.matrix[i][j] == 'D':
self.goal.x = j
self.goal.y = i
if self.matrix[i][j] == '1':
self.visited[i][j] = '2'
else:
self.visited[i][j] = '0'
def checkNeighbour(self, state, dir):
var = Coordinate(-1, -1)
if dir == 0:
if state.x-1 >= 0:
if self.visited[state.y][state.x-1] == '0':
var.x = state.x-1
var.y = state.y
if dir == 1:
if state.x+1 < self.column:
if self.visited[state.y][state.x+1] == '0':
var.x = state.x+1
var.y = state.y
if dir == 2:
if state.y-1 >= 0:
if self.visited[state.y-1][state.x] == '0':
var.x = state.x
var.y = state.y-1
if dir == 3:
if state.y+1 < self.row:
if self.visited[state.y+1][state.x] == '0':
var.x = state.x
var.y = state.y+1
return var
def bfs(matrix, row, column):
queue = Queue()
path = []
maze = Maze(matrix, row, column)
maze.load()
start = Node(maze.start, None)
goal = Node(Coordinate(-1, -1), None)
queue.add(start)
maze.visited[maze.start.y][maze.start.x] = '1'
while not queue.isEmpty():
current = queue.remove()
for i in range(4):
neighbour = maze.checkNeighbour(current.state, i)
if not (neighbour.x == -1) and not (neighbour.y == -1):
temp = Node(neighbour, queue.front-1)
queue.add(temp)
maze.visited[neighbour.y][neighbour.x] = '1'
if maze.goal.x == neighbour.x and maze.goal.y == neighbour.y:
goal = temp
break
if goal.state.x == -1 or goal.state.y == -1:
return None, None
else:
current = goal
while current.previous is not None:
path.append((current.state.x, current.state.y))
maze.matrix[current.state.y][current.state.x] = 'P'
current = queue.list[current.previous]
path.append((int(maze.start.x), int(maze.start.y)))
maze.matrix[maze.start.y][maze.start.x] = 'S'
maze.matrix[maze.goal.y][maze.goal.x] = 'D'
path = list(reversed(path))
del path[-1]
return maze.matrix, path
if __name__ == "__main__":
row = 15
column = 25
maze = mazeGenerator.Maze(column, row)
matrix = maze.matrix()
solutionMatrix, path = bfs(matrix, row, column)
if solutionMatrix is None:
print("No Solution Found!")
else:
for w in solutionMatrix:
for x in w:
print(x, end=" ")
print("")
print("\n")
path = list(path)
for elem in path:
print(elem)