Ailab0101.ipynb - Colab
Ailab0101.ipynb - Colab
while q:
current, path = q.popleft()
if current == end:
return path
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
neighbour = (current[0] + dx, current[1] + dy)
if 0 <= neighbour[0] < len(maze) and 0 <= neighbour[1] < len(maze[0]) and maze[
v.add(neighbour)
q.append((neighbour, path + [neighbour]))
while s:
current, path = s.pop()
if current == end:
p.append(path)
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
neighbour = (current[0] + dx, current[1] + dy)
if 0 <= neighbour[0] < len(maze) and 0 <= neighbour[1] < len(maze[0]) and maze[
v.add(neighbour)
s.append((neighbour, path + [neighbour]))
if not p:
return "No path found"
else:
return p[0]
def main():
maze = [
[1, 0, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 1, 1, 1],
[0, 0, 0, 1, 0],
[1, 1, 1, 1, 1]
]
start = (0, 0)
end = (4, 4)
Sneha Bharti
22051893
2025-01-06 20:23:07.565313+05:30
BFS Path: [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (3, 3), (4, 3), (4, 4
DFS Path: [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (3, 3), (4, 3), (4, 4
def neighnors(cell):
row,col=cell
result=[]
for dr