progs/knight2.scala
changeset 39 c6fe374a5fca
parent 8 ab77f6006f1f
child 42 a5106bc13db6
equal deleted inserted replaced
38:2c96963b2e5c 39:c6fe374a5fca
    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 && moves(n)(steps.head).contains(steps.last)) List(steps)
    29   if (steps.length ==  n * n) // && moves(n)(steps.head).contains(steps.last)) 
       
    30     List(steps)
    30   else 
    31   else 
    31     (for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) yield tour(n)(x :: steps)).toList.flatten
    32     (for (x <- moves(n)(steps.head).par; 
       
    33           if (!steps.contains(x))) yield tour(n)(x :: steps)).toList.flatten
    32 }
    34 }
    33 
    35 
    34 //val n = 8
    36 //val n = 8
    35 val n = 6
    37 val n = 6
    36 println(s"number simple tours: n = $n")
    38 println(s"number simple tours: n = $n")