templates3/knight1.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 02 Jan 2019 21:12:32 +0000
changeset 251 3dd550b9b0e3
parent 214 bc131735c940
child 296 12dc251fc47e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about finding Knight's tours
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//=====================================
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// If you need any auxiliary function, feel free to 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// implement it, but do not make any changes to the
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// templates below. Also have a look whether the functions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// at the end are of any help.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
type Pos = (Int, Int)    // a position on a chessboard 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
type Path = List[Pos]    // a path...a list of positions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
//(1) Complete the function that tests whether the position x
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
//    is inside the board and not yet element in the path.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
//def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ...
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
//(2) Complete the function that calculates for a position x
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
//    all legal onward moves that are not already in the path. 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
//    The moves should be ordered in a "clockwise" manner.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
//def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ...
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//some test cases
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
//assert(legal_moves(8, Nil, (2,2)) == 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//(3) Complete the two recursive functions below. 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//    They exhaustively search for knight's tours starting from the 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
//    given path. The first function counts all possible tours, 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//    and the second collects all tours in a list of paths.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
//def count_tours(dim: Int, path: Path) : Int = ...
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
//def enum_tours(dim: Int, path: Path) : List[Path] = ...
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
//(5) Implement a first-function that finds the first 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//    element, say x, in the list xs where f is not None. 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//    In that case Return f(x), otherwise None. If possible,
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
//    calculate f(x) only once.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
//def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ...
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// test cases
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
//
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
//first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)   // Some(List((4,0)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//first(List((1, 0),(2, 0),(3, 0)), foo)          // None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
//(6) Implement a function that uses the first-function from (5) for
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
//    trying out onward moves, and searches recursively for a
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
//    knight tour on a dim * dim-board.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//def first_tour(dim: Int, path: Path) : Option[Path] = ...
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
/* Helper functions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// for measuring time
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
def time_needed[T](code: => T) : T = {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  val start = System.nanoTime()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  val result = code
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  val end = System.nanoTime()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  result
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// can be called for example with
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
//     time_needed(count_tours(dim, List((0, 0))))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// in order to print out the time that is needed for 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// running count_tours
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
// for printing a board
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
def print_board(dim: Int, path: Path): Unit = {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  println
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  for (i <- 0 until dim) {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
    for (j <- 0 until dim) {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
    }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    println
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  } 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
*/