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