progs/knight1_sol.scala
changeset 45 8399976b77fe
child 50 9891c9fac37e
equal deleted inserted replaced
44:9cfa37c91444 45:8399976b77fe
       
     1 // Part 1 about finding and counting Knight's tours
       
     2 //==================================================
       
     3 
       
     4 type Pos = (Int, Int)    // a position on a chessboard 
       
     5 type Path = List[Pos]    // a path...a list of positions
       
     6 
       
     7 def print_board(dim: Int, path: Path): Unit = {
       
     8   println
       
     9   for (i <- 0 until dim) {
       
    10     for (j <- 0 until dim) {
       
    11       print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
       
    12     }
       
    13     println
       
    14   } 
       
    15 }
       
    16 
       
    17 def add_pair(x: Pos)(y: Pos): Pos = 
       
    18   (x._1 + y._1, x._2 + y._2)
       
    19 
       
    20 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
       
    21   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
       
    22 
       
    23 def moves(x: Pos): List[Pos] = 
       
    24   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
       
    25        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
       
    26 
       
    27 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
       
    28   moves(x).filter(is_legal(dim, path))
       
    29 
       
    30 legal_moves(8, Nil, (2,2))
       
    31 legal_moves(8, Nil, (7,7))
       
    32 
       
    33 
       
    34 def count_tours(dim: Int, path: Path): Int = {
       
    35   if (path.length == dim * dim) 1
       
    36   else 
       
    37     (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
       
    38 }
       
    39 
       
    40 def enum_tours(dim: Int, path: Path): List[Path] = {
       
    41   if (path.length == dim * dim) List(path)
       
    42   else 
       
    43     (for (x <- legal_moves(dim, path, path.head)) yield enum_tours(dim, x::path)).flatten
       
    44 }
       
    45 
       
    46 def count_all_tours(dim: Int): Int = {
       
    47   (for (i <- (0 until dim).toList; 
       
    48         j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))).sum
       
    49 }
       
    50 
       
    51 def enum_all_tours(dim: Int): List[Path] = {
       
    52   (for (i <- (0 until dim).toList; 
       
    53         j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
       
    54 }
       
    55 
       
    56 for (dim <- 1 to 5) {
       
    57   println(s"${dim} x ${dim} " + count_tours(dim, List((0, 0))))
       
    58 }
       
    59 
       
    60 for (dim <- 1 to 5) {
       
    61   println(s"${dim} x ${dim} " + count_all_tours(dim))
       
    62 }
       
    63 
       
    64 for (dim <- 1 to 5) {
       
    65   val ts = enum_tours(dim, List((0, 0)))
       
    66   println(s"${dim} x ${dim} ")   
       
    67   if (ts != Nil) {
       
    68     print_board(dim, ts.head)
       
    69     println(ts.head)
       
    70   }
       
    71 }
       
    72 
       
    73