I am solving this question:
Find the minimum number of steps required by the knight to move from the starting position to the end position in a nXn chess board. If the path does not exists, then return -1
I have written the below code:
def is_position_safe(n, cur_row, cur_col):
if 0 <= cur_row < n and 0 <= cur_col < n:
return True
else:
return False
def min_moves(n, cur_row, cur_col, end_row, end_col, visited):
if cur_row == end_row and cur_col == end_col:
return 0
key = tuple([cur_row, cur_col])
visited_temp = visited.copy()
if is_position_safe(n, cur_row, cur_col) and key not in visited:
visited_temp.append(key)
d1 = min_moves(n, cur_row - 2, cur_col - 1, end_row, end_col, visited_temp)
d2 = min_moves(n, cur_row - 2, cur_col + 1, end_row, end_col, visited_temp)
d3 = min_moves(n, cur_row - 1, cur_col - 2, end_row, end_col, visited_temp)
d4 = min_moves(n, cur_row - 1, cur_col + 2, end_row, end_col, visited_temp)
d5 = min_moves(n, cur_row + 1, cur_col - 2, end_row, end_col, visited_temp)
d6 = min_moves(n, cur_row + 1, cur_col + 2, end_row, end_col, visited_temp)
d7 = min_moves(n, cur_row + 2, cur_col - 1, end_row, end_col, visited_temp)
d8 = min_moves(n, cur_row + 2, cur_col + 1, end_row, end_col, visited_temp)
return min(d1, d2, d3, d4, d5, d6, d6, d7, d8) + 1
else:
return float('inf')
def min_knight_moves(n, cur_row, cur_col, end_row, end_col):
result = min_moves(n, cur_row, cur_col, end_row, end_col, [])
if result != float('inf'):
return result
else:
return -1
print(min_knight_moves(10, 9, 9, 6, 3))
It is not working in some cases. Example, for the above one it is returning me 5
, but the actual answer is 3
.
I am not able to understand where I am doing wrong. Could anybody please help me.
P.S.: I know there are existing solutions out there, by using BFS, but I was trying this problem with recursion. I am not quite sure, where I am wrong. Appreciate your help. Thanks!