progs/knight1_sol.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 08 Dec 2016 12:50:54 +0000
changeset 85 fd3f8581ce85
parent 50 9891c9fac37e
child 86 f8a781322499
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
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    30
assert(legal_moves(8, Nil, (2,2)) == 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    31
  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    32
assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    33
assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    34
  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    35
assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
85
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    36
assert(legal_moves(1, Nil, (0,0)) == List())
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    37
assert(legal_moves(2, Nil, (0,0)) == List())
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    38
assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    39
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
def count_tours(dim: Int, path: Path): Int = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  if (path.length == dim * dim) 1
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    (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
    45
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
def enum_tours(dim: Int, path: Path): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  if (path.length == dim * dim) List(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    (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
    51
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
def count_all_tours(dim: Int): Int = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  (for (i <- (0 until dim).toList; 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
        j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))).sum
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def enum_all_tours(dim: Int): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  (for (i <- (0 until dim).toList; 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
        j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  println(s"${dim} x ${dim} " + count_tours(dim, List((0, 0))))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  println(s"${dim} x ${dim} " + count_all_tours(dim))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  val ts = enum_tours(dim, List((0, 0)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  println(s"${dim} x ${dim} ")   
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  if (ts != Nil) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
    print_board(dim, ts.head)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
    println(ts.head)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80