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