diff -r fdc2c6fb7a24 -r 9891c9fac37e progs/knight3.scala --- a/progs/knight3.scala Tue Nov 15 23:08:09 2016 +0000 +++ b/progs/knight3.scala Wed Nov 16 14:37:18 2016 +0000 @@ -1,45 +1,65 @@ -import scala.util._ +// Part 3 about finding a single tour using the Warnsdorf Rule +//============================================================= + -def print_board(n: Int)(steps: List[(Int, Int)]): Unit = { - for (i <- 0 until n) { - for (j <- 0 until n) { - print(f"${steps.indexOf((i, j))}%3.0f ") +type Pos = (Int, Int) +type Path = List[Pos] + +def print_board(dim: Int, path: Path): Unit = { + println + for (i <- 0 until dim) { + for (j <- 0 until dim) { + print(f"${path.reverse.indexOf((i, j))}%3.0f ") } println } - //readLine() - System.exit(0) -} - -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)(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)) } -def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = - moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length) +def add_pair(x: Pos)(y: Pos): Pos = + (x._1 + y._1, x._2 + y._2) -moves(8)(1,3) -ordered_moves(8)(Nil)(1,3) -ordered_moves(8)(List((2, 4), (2, 6)))(1,3) +def is_legal(dim: Int, path: Path)(x: Pos): Boolean = + 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) + +def moves(x: Pos): List[Pos] = + List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), + (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)) -// non-circle tour parallel -def tour(n: Int)(steps: List[(Int, Int)]): Unit = { - if (steps.length == n * n && moves(n)(steps.head).contains(steps.last)) - print_board(n)(steps) - else - for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) tour(n)(x :: steps) +def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = + moves(x).filter(is_legal(dim, path)) + +def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = + legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length) + + +def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { + case Nil => None + case x::xs => { + val result = f(x) + if (result.isDefined) result else first(xs, f) + } } -val n = 7 -println(s"circle tour parallel: n = $n") + +def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = { + if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path) + else + first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path)) +} + +for (dim <- 1 to 6) { + val t = first_closed_tour_heuristics(dim, List((dim / 2, dim / 2))) + println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) +} -val starts = for (i <- 0 until n; j <- 0 until n) yield (i, j) -starts.par.foreach((x:(Int, Int)) => tour(n)(List(x))) +def first_tour_heuristics(dim: Int, path: Path): Option[Path] = { + if (path.length == dim * dim) Some(path) + else + first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path)) +} + +for (dim <- 1 to 50) { + val t = first_tour_heuristics(dim, List((dim / 2, dim / 2))) + println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) +}