MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / open_knight_tour_helper

Function open_knight_tour_helper

backtracking/knight_tour.py:49–68  ·  view source on GitHub ↗

Helper function to solve knight tour problem.

(
    board: list[list[int]], pos: tuple[int, int], curr: int
)

Source from the content-addressed store, hash-verified

47
48
49def open_knight_tour_helper(
50 board: list[list[int]], pos: tuple[int, int], curr: int
51) -> bool:
52 """
53 Helper function to solve knight tour problem.
54 """
55
56 if is_complete(board):
57 return True
58
59 for position in get_valid_pos(pos, len(board)):
60 y, x = position
61
62 if board[y][x] == 0:
63 board[y][x] = curr + 1
64 if open_knight_tour_helper(board, position, curr + 1):
65 return True
66 board[y][x] = 0
67
68 return False
69
70
71def open_knight_tour(n: int) -> list[list[int]]:

Callers 1

open_knight_tourFunction · 0.85

Calls 2

is_completeFunction · 0.85
get_valid_posFunction · 0.85

Tested by

no test coverage detected