| 
145
 | 
     1  | 
// Part 1 about finding and counting Knight's tours
  | 
| 
 | 
     2  | 
//==================================================
  | 
| 
 | 
     3  | 
  | 
| 
 | 
     4  | 
object CW7a {
 | 
| 
 | 
     5  | 
  | 
| 
 | 
     6  | 
type Pos = (Int, Int)    // a position on a chessboard 
  | 
| 
 | 
     7  | 
type Path = List[Pos]    // a path...a list of positions
  | 
| 
 | 
     8  | 
  | 
| 
 | 
     9  | 
def print_board(dim: Int, path: Path): Unit = {
 | 
| 
 | 
    10  | 
  println
  | 
| 
 | 
    11  | 
  for (i <- 0 until dim) {
 | 
| 
 | 
    12  | 
    for (j <- 0 until dim) {
 | 
| 
 | 
    13  | 
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
 | 
| 
 | 
    14  | 
    }
  | 
| 
 | 
    15  | 
    println
  | 
| 
 | 
    16  | 
  } 
  | 
| 
 | 
    17  | 
}
  | 
| 
 | 
    18  | 
  | 
| 
 | 
    19  | 
def add_pair(x: Pos)(y: Pos): Pos = 
  | 
| 
 | 
    20  | 
  (x._1 + y._1, x._2 + y._2)
  | 
| 
 | 
    21  | 
  | 
| 
 | 
    22  | 
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
  | 
| 
 | 
    23  | 
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
  | 
| 
 | 
    24  | 
  | 
| 
 | 
    25  | 
assert(is_legal(8, Nil)((3,4)) == true)
  | 
| 
 | 
    26  | 
assert(is_legal(8, List((4,1), (1,0)))((4,1)) == false)
  | 
| 
 | 
    27  | 
assert(is_legal(2, Nil)((0,0)) == true)
  | 
| 
 | 
    28  | 
  | 
| 
 | 
    29  | 
  | 
| 
 | 
    30  | 
  | 
| 
 | 
    31  | 
def moves(x: Pos): List[Pos] = 
  | 
| 
 | 
    32  | 
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
  | 
| 
 | 
    33  | 
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
  | 
| 
 | 
    34  | 
  | 
| 
 | 
    35  | 
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
  | 
| 
 | 
    36  | 
  moves(x).filter(is_legal(dim, path))
  | 
| 
 | 
    37  | 
  | 
| 
 | 
    38  | 
def count_tours(dim: Int, path: Path): Int = {
 | 
| 
 | 
    39  | 
  if (path.length == dim * dim) 1
  | 
| 
 | 
    40  | 
  else 
  | 
| 
 | 
    41  | 
    (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
  | 
| 
 | 
    42  | 
}
  | 
| 
 | 
    43  | 
  | 
| 
 | 
    44  | 
def enum_tours(dim: Int, path: Path): List[Path] = {
 | 
| 
 | 
    45  | 
  if (path.length == dim * dim) List(path)
  | 
| 
 | 
    46  | 
  else 
  | 
| 
 | 
    47  | 
    (for (x <- legal_moves(dim, path, path.head)) yield enum_tours(dim, x::path)).flatten
  | 
| 
 | 
    48  | 
}
  | 
| 
 | 
    49  | 
  | 
| 
 | 
    50  | 
def count_all_tours(dim: Int) = {
 | 
| 
 | 
    51  | 
  for (i <- (0 until dim).toList; 
  | 
| 
 | 
    52  | 
       j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
  | 
| 
 | 
    53  | 
}
  | 
| 
 | 
    54  | 
  | 
| 
 | 
    55  | 
def enum_all_tours(dim: Int): List[Path] = {
 | 
| 
 | 
    56  | 
  (for (i <- (0 until dim).toList; 
  | 
| 
 | 
    57  | 
        j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
  | 
| 
 | 
    58  | 
}
  | 
| 
 | 
    59  | 
  | 
| 
 | 
    60  | 
/*
  | 
| 
 | 
    61  | 
for (dim <- 1 to 5) {
 | 
| 
 | 
    62  | 
  println(s"${dim} x ${dim} " + count_tours(dim, List((0, 0))))
 | 
| 
 | 
    63  | 
}
  | 
| 
 | 
    64  | 
  | 
| 
 | 
    65  | 
for (dim <- 1 to 5) {
 | 
| 
 | 
    66  | 
  println(s"${dim} x ${dim} " + count_all_tours(dim))
 | 
| 
 | 
    67  | 
}
  | 
| 
 | 
    68  | 
  | 
| 
 | 
    69  | 
for (dim <- 1 to 5) {
 | 
| 
 | 
    70  | 
  val ts = enum_tours(dim, List((0, 0)))
  | 
| 
 | 
    71  | 
  println(s"${dim} x ${dim} ")   
 | 
| 
 | 
    72  | 
  if (ts != Nil) {
 | 
| 
 | 
    73  | 
    print_board(dim, ts.head)
  | 
| 
 | 
    74  | 
    println(ts.head)
  | 
| 
 | 
    75  | 
  }
  | 
| 
 | 
    76  | 
}
  | 
| 
 | 
    77  | 
*/ 
  | 
| 
 | 
    78  | 
  | 
| 
 | 
    79  | 
}
  |