50
|
1 |
// Part 1 about finding and counting Knight's tours
|
|
2 |
//==================================================
|
4
|
3 |
|
50
|
4 |
type Pos = (Int, Int) // a position on a chessboard
|
|
5 |
type Path = List[Pos] // a path...a list of positions
|
|
6 |
|
|
7 |
//(1a) Complete the function that tests whether the position
|
|
8 |
// is inside the board and not yet element in the path.
|
|
9 |
|
|
10 |
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = ...
|
4
|
11 |
|
|
12 |
|
50
|
13 |
//(1b) Complete the function that calculates for a position
|
|
14 |
// all legal onward moves that are not already in the path.
|
|
15 |
// The moves should be ordered in a "clockwise" order.
|
|
16 |
|
|
17 |
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = ...
|
4
|
18 |
|
50
|
19 |
//assert(legal_moves(8, Nil, (2,2)) ==
|
|
20 |
// List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
|
|
21 |
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
|
|
22 |
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) ==
|
|
23 |
// List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
|
|
24 |
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
|
4
|
25 |
|
|
26 |
|
50
|
27 |
//(1c) Complement the two recursive functions below.
|
|
28 |
// They exhaustively search for open tours starting from the
|
|
29 |
// given path. The first function counts all possible open tours,
|
|
30 |
// and the second collects all open tours in a list of paths.
|
4
|
31 |
|
50
|
32 |
def count_tours(dim: Int, path: Path): Int = ...
|
|
33 |
|
|
34 |
def enum_tours(dim: Int, path: Path): List[Path] = ...
|
|
35 |
|
|
36 |
|