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