progs/knight1_sol.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 14 Nov 2016 03:25:14 +0000
changeset 45 8399976b77fe
child 50 9891c9fac37e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about finding and counting Knight's tours
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================================
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
type Pos = (Int, Int)    // a position on a chessboard 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
type Path = List[Pos]    // a path...a list of positions
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def print_board(dim: Int, path: Path): Unit = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  for (i <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    for (j <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  } 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def add_pair(x: Pos)(y: Pos): Pos = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  (x._1 + y._1, x._2 + y._2)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def moves(x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  moves(x).filter(is_legal(dim, path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
legal_moves(8, Nil, (2,2))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
legal_moves(8, Nil, (7,7))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
def count_tours(dim: Int, path: Path): Int = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  if (path.length == dim * dim) 1
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
def enum_tours(dim: Int, path: Path): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  if (path.length == dim * dim) List(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
    (for (x <- legal_moves(dim, path, path.head)) yield enum_tours(dim, x::path)).flatten
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def count_all_tours(dim: Int): Int = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  (for (i <- (0 until dim).toList; 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
        j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))).sum
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
def enum_all_tours(dim: Int): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  (for (i <- (0 until dim).toList; 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
        j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  println(s"${dim} x ${dim} " + count_tours(dim, List((0, 0))))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  println(s"${dim} x ${dim} " + count_all_tours(dim))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  val ts = enum_tours(dim, List((0, 0)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  println(s"${dim} x ${dim} ")   
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  if (ts != Nil) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
    print_board(dim, ts.head)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
    println(ts.head)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73