-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.py
More file actions
52 lines (40 loc) · 1.38 KB
/
BFS.py
File metadata and controls
52 lines (40 loc) · 1.38 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
from collections import deque
import time
# Function to check if given position is valid on the chessboard
def is_valid(pos, N):
x, y = pos
if x < 0 or y < 0 or x >= N or y >= N:
return False
return True
# Function to find the minimum distance of a knight to reach the destination using BFS
def min_distance_knight(start, end, N):
dx = [-2, -2, -1, -1, 1, 1, 2, 2]
dy = [-1, 1, -2, 2, -2, 2, -1, 1]
visited = [[False for j in range(N)] for i in range(N)]
distance = [[0 for j in range(N)] for i in range(N)]
x, y = start
visited[x][y] = True
q = deque([(x, y)])
while q:
curr_x, curr_y = q.popleft()
if (curr_x, curr_y) == end:
return distance[curr_x][curr_y]
for i in range(8):
new_x, new_y = curr_x + dx[i], curr_y + dy[i]
if is_valid((new_x, new_y), N) and not visited[new_x][new_y]:
visited[new_x][new_y] = True
distance[new_x][new_y] = distance[curr_x][curr_y] + 1
q.append((new_x, new_y))
return -1
# Example usage
start = (0, 0)
end = (7, 7)
N = 8
start_time = time.time()
min_distance = min_distance_knight(start, end, N)
end_time = time.time()
if min_distance != -1:
print("Minimum distance of knight to reach destination:", min_distance)
else:
print("No path exists.")
print("Time taken:", end_time - start_time, "seconds")