progs/knight2.scala
changeset 44 9cfa37c91444
parent 43 ba4081c70de4
child 50 9891c9fac37e
equal deleted inserted replaced
43:ba4081c70de4 44:9cfa37c91444
     1 import scala.util._
     1 
     2 type Pos = (Int, Int)
     2 type Pos = (Int, Int)
       
     3 type Path = List[Pos]
       
     4 
       
     5 def print_board(dim: Int, path: Path): Unit = {
       
     6   println
       
     7   for (i <- 0 until dim) {
       
     8     for (j <- 0 until dim) {
       
     9       print(f"${path.indexOf((i, j))}%3.0f ")
       
    10     }
       
    11     println
       
    12   } 
       
    13 }
       
    14 
     3 
    15 
     4 def add_pair(x: Pos)(y: Pos): Pos = 
    16 def add_pair(x: Pos)(y: Pos): Pos = 
     5   (x._1 + y._1, x._2 + y._2)
    17   (x._1 + y._1, x._2 + y._2)
     6 
    18 
     7 def is_legal(dim: Int, path: List[Pos])(x: Pos): Boolean = 
    19 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
     8   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
    20   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
     9 
    21 
    10 def moves(x: Pos): List[Pos] = {
    22 def moves(x: Pos): List[Pos] = {
    11   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
    23   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
    12        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    24        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    13 }
    25 }
    14 
    26 
    15 def legal_moves(dim: Int, path: List[Pos], x: Pos): List[Pos] = {
    27 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
    16   moves(x).filter(is_legal(dim, path))
    28   moves(x).filter(is_legal(dim, path))
    17 }
    29 
    18 
    30 
    19 
    31 
    20 // non-circle tours
    32 // non-circle tours
    21 /*
    33 /*
    22 def tour(dim: Int, path: List[Pos]): List[List[Pos]] = {
    34 def tour(dim: Int, path: List[Pos]): List[List[Pos]] = {
    25   else 
    37   else 
    26     (for (x <- legal_moves(dim, path, path.head)) yield tour(dim, x::path)).flatten
    38     (for (x <- legal_moves(dim, path, path.head)) yield tour(dim, x::path)).flatten
    27 }
    39 }
    28 */
    40 */
    29 
    41 
    30 def tour(dim: Int, path: List[Pos]): Int = {
    42 def tour(dim: Int, path: Path): Int = {
    31   if (path.length == dim * dim) 1
    43   if (path.length == dim * dim) 1
    32   else 
    44   else 
    33     (for (x <- legal_moves(dim, path, path.head).par) yield tour(dim, x::path)).sum
    45     (for (x <- legal_moves(dim, path, path.head) yield tour(dim, x::path))).sum
    34 }
    46 }
    35 
    47 
       
    48 
       
    49 def dtour(dim: Int): List[List[Pos]] = {
       
    50   var counter = 100000000
       
    51 
       
    52   def etour(dim: Int, path: List[Pos]): List[List[Pos]] = {
       
    53     counter = counter - 1
       
    54     if (counter <= 0) List() else
       
    55       if (path.length == dim * dim) List(path)
       
    56       else 
       
    57         (for (x <- legal_moves(dim, path, path.head)) yield etour(dim, x::path)).flatten
       
    58   }
       
    59 
       
    60   (for (i <- (0 until dim).toList; 
       
    61         j <- (0 until dim).toList) yield etour(dim, List((i, j)))).flatten
       
    62 }
       
    63 
       
    64 
       
    65 
    36 //val n = 8
    66 //val n = 8
    37 val n = 6
    67 val n = 5
    38 println(s"number simple tours: n = $n")
    68 println(s"number simple tours: n = $n")
    39 
    69 
    40 println(tour(n, List((0, 0))))
    70 //println(etour(n, List((0, 0))).size)
    41 
    71 
    42 /*
    72 
    43 for (d <- 1 to 6) {
    73 
    44   println(s"${d} x ${d} " + (for (i <- 0 until d; j <- 0 until d) yield tour(d, List((i, j)))).sum)
    74 for (d <- 9 to 9) {
    45 } 
    75   println(s"${d} x ${d} " + dtour(d).length)
    46 */
    76 }
       
    77 
       
    78