progs/knight1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 01 Aug 2019 09:48:34 +0100
changeset 268 e43f7e92ba26
parent 213 f968188d4a9b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
     1
// Part 1 about finding Knight's tours
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
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
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
     7
// for measuring time
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
     8
def time_needed[T](code: => T) : T = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
     9
  val start = System.nanoTime()
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    10
  val result = code
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    11
  val end = System.nanoTime()
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    12
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    13
  result
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    14
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    15
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    16
// for printing a board
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    17
def print_board(dim: Int, path: Path): Unit = {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    18
  println
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    19
  for (i <- 0 until dim) {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    20
    for (j <- 0 until dim) {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    21
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
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
    println
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    24
  } 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    25
}
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    26
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    27
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    28
// 1 mark
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    29
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    30
def is_legal(dim: Int, path: Path, x: Pos): Boolean = 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    31
  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
    32
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    33
assert(is_legal(8, Nil, (3, 4)) == true)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    34
assert(is_legal(8, List((4, 1), (1, 0)), (4, 1)) == false)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    35
assert(is_legal(2, Nil, (0, 0)) == true)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    38
def add_pair(x: Pos, y: Pos): Pos = 
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    39
  (x._1 + y._1, x._2 + y._2)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    40
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    41
def moves(x: Pos): List[Pos] = 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    42
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    43
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x, _))
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    44
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    45
// 1 mark
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    46
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    47
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    48
  moves(x).filter(is_legal(dim, path, _))
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    50
assert(legal_moves(8, Nil, (2,2)) == 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    51
  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
    52
assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    53
assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    54
  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
    55
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
    56
assert(legal_moves(1, Nil, (0,0)) == List())
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    57
assert(legal_moves(2, Nil, (0,0)) == List())
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    58
assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    59
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    60
// 2 marks
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    61
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    62
def count_tours(dim: Int, path: Path): Int = {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    63
  if (path.length == dim * dim) 1
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    64
  else 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    65
    (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
    66
}
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    67
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    68
def enum_tours(dim: Int, path: Path): List[Path] = {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    69
  if (path.length == dim * dim) List(path)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    70
  else 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    71
    (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
    72
}
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    73
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    74
// as far as tasks go
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    75
// test cases for CW
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    79
def count_all_tours(dim: Int) = {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    80
  for (i <- (0 until dim).toList; 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    81
       j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    82
}
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    83
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    84
def enum_all_tours(dim: Int): List[Path] = {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    85
  (for (i <- (0 until dim).toList; 
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    86
        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
    87
}
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    88
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    89
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    90
println("Number of tours starting from (0, 0)")
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    91
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    92
for (dim <- 1 to 5) {
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    93
  println(s"${dim} x ${dim} " + time_needed(0, count_tours(dim, List((0, 0)))))
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    94
}
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    95
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    96
println("Number of tours starting from all fields")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    97
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    98
for (dim <- 1 to 5) {
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
    99
  println(s"${dim} x ${dim} " + time_needed(0, count_all_tours(dim)))
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   100
}
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   101
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   102
for (dim <- 1 to 5) {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   103
  val ts = enum_tours(dim, List((0, 0)))
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   104
  println(s"${dim} x ${dim} ")   
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   105
  if (ts != Nil) {
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   106
    print_board(dim, ts.head)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   107
    println(ts.head)
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   108
  }
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   109
}
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   110
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   111
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   112
// 1 mark
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   113
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   114
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   115
  case Nil => None
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   116
  case x::xs => {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   117
    val result = f(x)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   118
    if (result.isDefined) result else first(xs, f)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   119
  }
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   120
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   121
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   122
// test cases
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   123
def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   124
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   125
first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   126
first(List((1, 0),(2, 0),(3, 0)), foo)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   127
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   128
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   129
// 1 mark
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   130
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   131
def first_tour(dim: Int, path: Path): Option[Path] = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   132
  if (path.length == dim * dim) Some(path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   133
  else
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   134
    first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   135
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   136
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   137
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   138
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   139
/*
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   140
for (dim <- 1 to 8) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   141
  val t = first_tour(dim, List((0, 0)))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   142
  println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   143
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   144
*/
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   145
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   146
// 15 secs for 8 x 8
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   147
val ts1 = time_needed(0,first_tour(8, List((0, 0))).get)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   148
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   149
// no result for 4 x 4
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   150
val ts2 = time_needed(0, first_tour(4, List((0, 0))))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   151
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   152
// 0.3 secs for 6 x 6
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   153
val ts3 = time_needed(0, first_tour(6, List((0, 0))))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   154
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   155
// 15 secs for 8 x 8
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   156
time_needed(0, print_board(8, first_tour(8, List((0, 0))).get))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 212
diff changeset
   157