progs/knight1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 24 Nov 2016 11:43:07 +0000
changeset 71 19dff7218b0d
parent 53 9f8751912560
child 212 4bda49ec24da
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     1
// Part 1 about finding and counting Knight's tours
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     2
//==================================================
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     4
type Pos = (Int, Int)    // a position on a chessboard 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     5
type Path = List[Pos]    // a path...a list of positions
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     6
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     7
//(1a) Complete the function that tests whether the position 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     8
// is inside the board and not yet element in the path.
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     9
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    10
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = ...
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    13
//(1b) Complete the function that calculates for a position 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    14
// all legal onward moves that are not already in the path. 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    15
// The moves should be ordered in a "clockwise" order.
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    16
 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    17
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = ...
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    19
//assert(legal_moves(8, Nil, (2,2)) == 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    20
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    21
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    22
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    23
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    24
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    27
//(1c) Complete the two recursive functions below. 
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    28
// They exhaustively search for open tours starting from the 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    29
// given path. The first function counts all possible open tours, 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    30
// and the second collects all open tours in a list of paths.
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    32
def count_tours(dim: Int, path: Path): Int = ...
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    33
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    34
def enum_tours(dim: Int, path: Path): List[Path] = ...
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    35
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    36