Skip to content

Commit ba68db9

Browse files
committed
feat(bfs): inherit from grid2d class
1 parent fbee996 commit ba68db9

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

algorithms/bfs.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@
88
"""
99
import numpy as np
1010
from queue import Queue
11+
from grid2d import Grid2D
1112

12-
class BFS2D:
13+
class BFS2D(Grid2D):
1314

1415
def __init__(self, grid_width, grid_height, visited=None):
15-
self.grid_width = grid_width
16-
self.grid_height = grid_height
17-
if visited is None:
18-
self.visited = np.zeros((grid_height, grid_width))
19-
else:
20-
self.visited = visited
21-
self.steps_taken = np.zeros((grid_height, grid_width))
16+
Grid2D.__init__(self, grid_width, grid_height, visited)
2217
self.queue = Queue()
2318

24-
2519
def bfs(self, x, y, steps=0):
2620
"""Breadth-First Search algorithm that visits every cell in a 2D grid, where adjacent cells are defined as up to eight cells next to the current cell (can move diagonally).
2721
"""
@@ -59,13 +53,6 @@ def bfs(self, x, y, steps=0):
5953
self.steps_taken[celly, cellx] = steps
6054
steps += 1
6155

62-
def neighbour(self, x, y):
63-
neighbours = []
64-
for dx in [-1,0,1]:
65-
for dy in [-1,0,1]:
66-
neighbours.append([x + dx, y + dy])
67-
return neighbours
68-
6956
bfs = BFS2D(4, 3)
7057
bfs.bfs(1,2)
7158
print(bfs.steps_taken)

0 commit comments

Comments
 (0)