progs/knight3.scala
changeset 53 9f8751912560
parent 50 9891c9fac37e
child 213 f968188d4a9b
equal deleted inserted replaced
51:0e60e6c24b99 53:9f8751912560
     1 // Part 3 about finding a single tour using the Warnsdorf Rule
     1 // Part 3 about finding a single tour using the Warnsdorf Rule
     2 //=============================================================
     2 //=============================================================
     3 
     3 
       
     4 // copy any function you need from files knight1.scala and
       
     5 // knight2.scala
     4 
     6 
     5 type Pos = (Int, Int)
     7 type Pos = (Int, Int)    // a position on a chessboard 
     6 type Path = List[Pos]
     8 type Path = List[Pos]    // a path...a list of positions
     7 
     9 
     8 def print_board(dim: Int, path: Path): Unit = {
    10 //(3a) Complete the function that calculates a list of onward
     9   println
    11 // moves like in (1b) but orders them according to the Warnsdorf’s 
    10   for (i <- 0 until dim) {
    12 // rule. That means moves with the fewest legal onward moves 
    11     for (j <- 0 until dim) {
    13 // should come first.
    12       print(f"${path.reverse.indexOf((i, j))}%3.0f ")
       
    13     }
       
    14     println
       
    15   } 
       
    16 }
       
    17 
    14 
    18 def add_pair(x: Pos)(y: Pos): Pos = 
    15 def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = ..
    19   (x._1 + y._1, x._2 + y._2)
       
    20 
    16 
    21 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
    17 //(3b) Complete the function that searches for a single *closed* 
    22   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
    18 // tour using the ordered moves function.
    23 
    19 
    24 def moves(x: Pos): List[Pos] = 
    20 def first_closed_tour_heuristic(dim: Int, path: Path): Option[Path] = ...
    25   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
       
    26        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
       
    27 
    21 
    28 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
    22 //(3c) Same as (3b) but searches for *open* tours.
    29   moves(x).filter(is_legal(dim, path))
       
    30 
    23 
    31 def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
    24 def first_tour_heuristic(dim: Int, path: Path): Option[Path] = ...
    32   legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
       
    33 
       
    34 
       
    35 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
       
    36   case Nil => None
       
    37   case x::xs => {
       
    38     val result = f(x)
       
    39     if (result.isDefined) result else first(xs, f)
       
    40   }
       
    41 }
       
    42 
       
    43 
       
    44 def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = {
       
    45   if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
       
    46   else
       
    47     first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path))
       
    48 }
       
    49 
       
    50 for (dim <- 1 to 6) {
       
    51   val t = first_closed_tour_heuristics(dim, List((dim / 2, dim / 2)))
       
    52   println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
       
    53 }
       
    54 
       
    55 
       
    56 def first_tour_heuristics(dim: Int, path: Path): Option[Path] = {
       
    57   if (path.length == dim * dim) Some(path)
       
    58   else
       
    59     first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path))
       
    60 }
       
    61 
       
    62 for (dim <- 1 to 50) {
       
    63   val t = first_tour_heuristics(dim, List((dim / 2, dim / 2)))
       
    64   println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
       
    65 }