Skip to content

Commit 2540240

Browse files
add type hints
1 parent e6616a9 commit 2540240

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

searches/breadth_first_search_maze.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# MAZE TRAVERSAL
21
"""
32
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It begins at a root node (or an arbitrary node in a graph) and explores all of the neighbor nodes at the present depth before moving on to the nodes at the next depth level.
4-
This implementation of BFS is used to traverse a maze represented as a 2D grid. The maze contains walls (#), open paths ( ), a starting point (O), and a target point (X). The algorithm finds the shortest path from the starting point to the target point while avoiding walls."""
3+
This implementation of BFS is used to traverse a maze represented as a 2D grid. The maze contains walls (#), open paths ( ), a starting point (O), and a target point (X). The algorithm finds the shortest path from the starting point to the target point while avoiding walls.
4+
"""
5+
56
import curses
67
from curses import wrapper
78
import queue
@@ -20,7 +21,7 @@
2021
# ["#", "#", "#", "#", "#", "#", "#", "X", "#"]
2122
# ]
2223

23-
maze = [
24+
maze: list[list[str]] = [
2425
["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"],
2526
["#", "O", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
2627
["#", "#", "#", "#", "#", " ", "#", "#", "#", "#", "#", "#", "#", " ", "#", "#", "#", "#", " ", "#"],
@@ -43,7 +44,7 @@
4344
["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"]
4445
]
4546

46-
def print_maze(maze, stdscr, visited, path=[]):
47+
def print_maze(maze: list[list[str]], stdscr: curses.window, visited: set[tuple[int, int]], path: list[tuple[int, int]] = []) -> None:
4748
blue = curses.color_pair(1)
4849
red = curses.color_pair(2)
4950
green = curses.color_pair(3)
@@ -58,14 +59,14 @@ def print_maze(maze, stdscr, visited, path=[]):
5859
stdscr.addstr(row, column*2, j, blue)
5960

6061

61-
def find(maze, start): #to check and return starting position in maze
62+
def find(maze: list[list[str]], start: str) -> tuple[int, int] | None: #to check and return starting position in maze
6263
for row, i in enumerate(maze):
6364
for column, j in enumerate(i):
6465
if j == start:
6566
return (row,column) # return tuple of row, column location of element in the maze
6667
return None
6768

68-
def find_neighbours(maze, row, col): #search and return each neighbour of a particular cell, without checking if its a wall or not
69+
def find_neighbours(maze: list[list[str]], row: int, col: int) -> list[tuple[int, int]]:
6970
neighbours = []
7071

7172
if row > 0: # for UP
@@ -79,7 +80,7 @@ def find_neighbours(maze, row, col): #search and return each neighbour of a part
7980
return neighbours
8081

8182

82-
def traverse(maze, stdscr): #implementing bfs traversal
83+
def traverse(maze: list[list[str]], stdscr: curses.window) -> list[tuple[int, int]] | None: #implementing bfs traversal
8384
start = "O"
8485
target = "X"
8586
start_pos = find(maze, start)
@@ -118,7 +119,7 @@ def traverse(maze, stdscr): #implementing bfs traversal
118119
visited.add(nbr)
119120

120121

121-
def main(stdscr):
122+
def main(stdscr: curses.window) -> None:
122123
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK)
123124
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
124125
curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)

0 commit comments

Comments
 (0)