|
| 1 | +package misc |
| 2 | +// P91 (**) Knight’s tour. |
| 3 | +// Another famous problem is this one: How can a knight jump on an NxN chessboard in such a way |
| 4 | +// that it visits every square exactly once? |
| 5 | +// |
| 6 | +// Hints: Represent the squares by pairs of their coordinates of the form (X,Y), where both |
| 7 | +// X and Y are integers between 1 and N. (Alternately, define a Point class for the same purpose.) |
| 8 | +// Write a function jumps(N,(X,Y)) to list the squares that a knight can jump to from (X,Y) on a |
| 9 | +// NxN chessboard.And finally, represent the solution of our problem as a list of knight positions |
| 10 | +// (the knight’s tour). |
| 11 | +// |
| 12 | +// It might be nice to find more than one tour, but a computer will take a long time trying to find |
| 13 | +// them all at once. Can you make a lazy list that only calculates the tours as needed? |
| 14 | +// |
| 15 | +// Can you find only "closed tours", where the knight can jump from its final position back to its |
| 16 | +// starting position? |
| 17 | +object P91: |
| 18 | + private type Square = (x: Int, y: Int) |
| 19 | + private val moves: LazyList[Square] = LazyList( |
| 20 | + (2, 1), |
| 21 | + (1, 2), |
| 22 | + (-1, 2), |
| 23 | + (-2, 1), |
| 24 | + (-2, -1), |
| 25 | + (-1, -2), |
| 26 | + (1, -2), |
| 27 | + (2, -1) |
| 28 | + ) |
| 29 | + |
| 30 | + def allSquares(n: Int): LazyList[Square] = LazyList |
| 31 | + .iterate((x = 0, y = 0)) { curr => |
| 32 | + if curr.y == n - 1 then (curr.x + 1, 0) |
| 33 | + else (curr.x, curr.y + 1) |
| 34 | + } |
| 35 | + .takeWhile(_.x < n) |
| 36 | + |
| 37 | + def allKnightsTours(n: Int): LazyList[List[Square]] = |
| 38 | + allSquares(n) |
| 39 | + .map(knightsTour(n, Map.empty, _)) |
| 40 | + |
| 41 | + private def knightsTour(n: Int, visited: Map[Square, Int], curr: Square): List[Square] = |
| 42 | + // To find a closed tour, also check if the next moves contain the start square. |
| 43 | + if visited.size == n * n - 1 then visited.toList.sortBy(_._2).map(_._1) :+ curr |
| 44 | + else |
| 45 | + /* |
| 46 | + It is imperative that nextMoves is lazy, |
| 47 | + otherwise even after finding a tour, |
| 48 | + it keeps of trying to find others. |
| 49 | + */ |
| 50 | + val vs = visited + (curr -> (visited.size + 1)) |
| 51 | + nextMoves(n, vs, curr) |
| 52 | + .sortBy(mv => nextMoves(n, vs, mv).size) // Warnsdorff's heuristic |
| 53 | + .map(nxt => knightsTour(n, vs, nxt)) |
| 54 | + .find(_.size == n * n) |
| 55 | + .getOrElse(List.empty) |
| 56 | + |
| 57 | + private def nextMoves(n: Int, visited: Map[Square, Int], curr: Square): LazyList[Square] = |
| 58 | + moves |
| 59 | + .filter { mv => |
| 60 | + val x = curr.x + mv.x |
| 61 | + val y = curr.y + mv.y |
| 62 | + x >= 0 && x < n && y >= 0 && y < n && !visited.contains((x, y)) |
| 63 | + } |
| 64 | + .map(mv => (curr.x + mv.x, curr.y + mv.y)) |
0 commit comments