templates3-bak/knight1.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 16 Dec 2018 21:43:14 +0000
changeset 249 1997cfcd6334
parent 212 4bda49ec24da
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
146
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about finding and counting Knight's tours
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================================
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW7a {
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)    // a position on a chessboard 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]    // a path...a list of positions
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//(1a) Complete the function that tests whether the position 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
//     is inside the board and not yet element in the path.
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    12
//def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ...
146
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
//(1b) Complete the function that calculates for a position 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
//     all legal onward moves that are not already in the path. 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
//     The moves should be ordered in a "clockwise" manner.
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
//def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ...
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
//some test cases
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
//
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
//assert(legal_moves(8, Nil, (2,2)) == 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//(1c) Complete the two recursive functions below. 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
//     They exhaustively search for knight's tours starting from the 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
//     given path. The first function counts all possible tours, 
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//     and the second collects all tours in a list of paths.
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
//def count_tours(dim: Int, path: Path) : Int = ...
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//def enum_tours(dim: Int, path: Path) : List[Path] = ...
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
61d9a5ac6430 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}