--- a/progs/knight2.scala Wed Nov 02 13:26:26 2016 +0000
+++ b/progs/knight2.scala Thu Nov 03 00:53:53 2016 +0000
@@ -24,22 +24,15 @@
(-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
}
-def ordered_moves(n: Int)(steps: List[Pos])(x : Pos): List[Pos] =
- moves(n)(x).sortBy((x: Pos) => moves(n)(x).filterNot(steps.contains(_)).length)
-
-moves(8)(1,3)
-ordered_moves(8)(Nil)(1,3)
-ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
-
-// non-circle tour
-def tour(n: Int)(steps: List[Pos]): Unit = {
- if (steps.length == n * n)
- print_board(n)(steps)
+// non-circle tours
+def tour(n: Int)(steps: List[Pos]): List[List[Pos]] = {
+ if (steps.length == n * n) List(steps)
else
- for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps)
+ (for (x <- moves(n)(steps.head); if (!steps.contains(x))) yield tour(n)(x :: steps)).flatten
}
-val n = 8
-println(s"simple tour: n = $n")
+//val n = 8
+val n = 6
+println(s"number simple tours: n = $n")
-for (i <- 0 until n; j <- 0 until n) tour(n)(List((i, j)))
+println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size)