progs/knight1.scala
changeset 50 9891c9fac37e
parent 4 f31c22f4f104
child 53 9f8751912560
--- a/progs/knight1.scala	Tue Nov 15 23:08:09 2016 +0000
+++ b/progs/knight1.scala	Wed Nov 16 14:37:18 2016 +0000
@@ -1,76 +1,36 @@
-import scala.util._
+// Part 1 about finding and counting Knight's tours
+//==================================================
 
-class Computation[A,B](value: A, function: A => B) {
-  lazy val result = function(value)
-}
+type Pos = (Int, Int)    // a position on a chessboard 
+type Path = List[Pos]    // a path...a list of positions
+
+//(1a) Complete the function that tests whether the position 
+// is inside the board and not yet element in the path.
+
+def is_legal(dim: Int, path: Path)(x: Pos): Boolean = ...
 
 
-def print_board(n: Int)(steps: List[(Int, Int)]): Unit = {
-  println
-  for (i <- 0 until n) {
-    for (j <- 0 until n) {
-      print(f"${steps.indexOf((i, j))}%3.0f ")
-    }
-    println
-  } 
-}
-
-def add_pair(x: (Int, Int))(y: (Int, Int)) = 
-  (x._1 + y._1, x._2 + y._2)
-
-def is_legal(n: Int)(x: (Int, Int)) = 
-  0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
-
-def moves(n: Int)(steps: List[(Int, Int)])(x: (Int, Int)): List[(Int, Int)] = {
-  List((1, 2),(2, 1),(2, -1),(1, -2),
-       (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)).filterNot(steps.contains(_))
-}
-
-def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = 
-  moves(n)(steps)(x).sortBy(moves(n)(steps)(_).length)
-
-moves(8)(Nil)(1,3)
-ordered_moves(8)(Nil)(1,3)
-ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
+//(1b) Complete the function that calculates for a position 
+// all legal onward moves that are not already in the path. 
+// The moves should be ordered in a "clockwise" order.
+ 
+def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = ...
 
-def first[A, B](xs: List[A], f: A => Set[B]): Set[B] = xs match {
-  case Nil => Set()
-  case x::xs => {
-    val result = f(x)
-    if (result == Set()) first(xs, f) else result
-  }
-}
-
-// non-circular tour
-def tour(n: Int)(steps: List[(Int, Int)]): Option[List[(Int, Int)]] = {
-  if (steps.length ==  n * n) Some(steps) 
-  else 
-    { val list = moves(n)(steps)(steps.head) map (x => new Computation(x, ((x:(Int, Int)) => tour(n)(x::steps))))
-      val found = list.par find (_.result.isDefined)
-      found map (_.result.get)
-    }
-}
-
-val n = 6
-println(s"simple tour: n = $n")
-
-val starts = for (i <- (0 until n).toList; 
-                  j <- (0 until n).toList) yield new Computation ((i, j), ((x:(Int, Int)) => tour(n)(x::Nil)))
-
-val found = starts.par find (_.result.isDefined)
-print_board(n)((found map (_.result.get)).get)
-
-//for measuring time
-def time_needed[T](i: Int, code: => T) = {
-  val start = System.nanoTime()
-  for (j <- 1 to i) code
-  val end = System.nanoTime()
-  (end - start)/(i * 1.0e9)
-}
-
-//for (i <- 1 to 20) {
-//  println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
-//}
+//assert(legal_moves(8, Nil, (2,2)) == 
+//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
+//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
+//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
+//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
+//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
 
 
+//(1c) Complement the two recursive functions below. 
+// They exhaustively search for open tours starting from the 
+// given path. The first function counts all possible open tours, 
+// and the second collects all open tours in a list of paths.
 
+def count_tours(dim: Int, path: Path): Int = ...
+
+def enum_tours(dim: Int, path: Path): List[Path] = ...
+
+