progs/knight2.scala
changeset 8 ab77f6006f1f
parent 7 7a5a29a32568
child 39 c6fe374a5fca
equal deleted inserted replaced
7:7a5a29a32568 8:ab77f6006f1f
    24        (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
    24        (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
    25 }
    25 }
    26 
    26 
    27 // non-circle tours
    27 // non-circle tours
    28 def tour(n: Int)(steps: List[Pos]): List[List[Pos]] = {
    28 def tour(n: Int)(steps: List[Pos]): List[List[Pos]] = {
    29   if (steps.length ==  n * n) List(steps)
    29   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last)) List(steps)
    30   else 
    30   else 
    31     (for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) yield tour(n)(x :: steps)).toList.flatten
    31     (for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) yield tour(n)(x :: steps)).toList.flatten
    32 }
    32 }
    33 
    33 
    34 //val n = 8
    34 //val n = 8
    35 val n = 6
    35 val n = 6
    36 println(s"number simple tours: n = $n")
    36 println(s"number simple tours: n = $n")
    37 
    37 
    38 println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size) 
    38 println(tour(n)(List((1,1))).distinct.size)
       
    39 //println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size)