4
4
5
5
6
6
def get_valid_pos (position : Tuple [int ], n : int ) -> List [Tuple [int ]]:
7
- '''
7
+ """
8
8
Find all the valid positions a knight can move to from the current position.
9
9
10
10
>>> get_valid_pos((1, 3), 4)
11
11
[(2, 1), (0, 1), (3, 2)]
12
- '''
12
+ """
13
13
14
14
y , x = position
15
15
positions = [
@@ -20,7 +20,7 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
20
20
(y + 2 , x + 1 ),
21
21
(y + 2 , x - 1 ),
22
22
(y - 2 , x + 1 ),
23
- (y - 2 , x - 1 )
23
+ (y - 2 , x - 1 ),
24
24
]
25
25
permissible_positions = []
26
26
@@ -33,23 +33,23 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
33
33
34
34
35
35
def is_complete (board : List [List [int ]]) -> bool :
36
- '''
36
+ """
37
37
Check if the board (matrix) has been completely filled with non-zero values.
38
38
39
39
>>> is_complete([[1]])
40
40
True
41
41
42
42
>>> is_complete([[1, 2], [3, 0]])
43
43
False
44
- '''
44
+ """
45
45
46
46
return not any (elem == 0 for row in board for elem in row )
47
47
48
48
49
49
def open_knight_tour_helper (board : List [List [int ]], pos : Tuple [int ], curr : int ) -> bool :
50
- '''
50
+ """
51
51
Helper function to solve knight tour problem.
52
- '''
52
+ """
53
53
54
54
if is_complete (board ):
55
55
return True
@@ -67,7 +67,7 @@ def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int)
67
67
68
68
69
69
def open_knight_tour (n : int ) -> List [List [int ]]:
70
- '''
70
+ """
71
71
Find the solution for the knight tour problem for a board of size n. Raises
72
72
ValueError if the tour cannot be performed for the given size.
73
73
@@ -78,7 +78,7 @@ def open_knight_tour(n: int) -> List[List[int]]:
78
78
Traceback (most recent call last):
79
79
...
80
80
ValueError: Open Kight Tour cannot be performed on a board of size 2
81
- '''
81
+ """
82
82
83
83
board = [[0 for i in range (n )] for j in range (n )]
84
84
0 commit comments