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