testing3/knight1.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 29 Oct 2019 11:11:44 +0000
changeset 280 a56a6c28b700
parent 247 87047208d5f4
child 284 fc20e5f83f0e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
247
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
     1
// Part 1 about finding and counting Knight's tours
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
     2
//==================================================
220
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
247
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
     4
//object CW8a {   // for preparing the jar
222
ec9cbf969edf updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
     5
220
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)    // a position on a chessboard 
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]    // a path...a list of positions
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
247
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    10
// for measuring time in the JAR
220
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
def time_needed[T](code: => T) : T = {
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  val start = System.nanoTime()
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  val result = code
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  val end = System.nanoTime()
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  result
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
// for printing a board
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def print_board(dim: Int, path: Path): Unit = {
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  println
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  for (i <- 0 until dim) {
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
    for (j <- 0 until dim) {
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
    }
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    println
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  } 
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
247
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    30
def is_legal(dim: Int, path: Path, x: Pos): Boolean = 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    31
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
220
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
247
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    33
// testcases
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    34
//assert(is_legal(8, Nil, (3, 4)) == true)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    35
//assert(is_legal(8, List((4, 1), (1, 0)), (4, 1)) == false)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    36
//assert(is_legal(2, Nil, (0, 0)) == true)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    37
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    38
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    39
def add_pair(x: Pos, y: Pos): Pos = 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    40
  (x._1 + y._1, x._2 + y._2)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    41
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    42
def moves(x: Pos): List[Pos] = 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    43
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    44
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x, _))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    45
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    46
// 1 mark
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    47
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    48
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    49
  moves(x).filter(is_legal(dim, path, _))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    50
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    51
// testcases
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    52
//assert(legal_moves(8, Nil, (2,2)) == 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    53
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    54
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    55
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    56
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    57
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    58
//assert(legal_moves(1, Nil, (0,0)) == List())
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    59
//assert(legal_moves(2, Nil, (0,0)) == List())
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    60
//assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    61
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    62
// 2 marks
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    63
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    64
def tcount_tours(dim: Int, path: Path): Int = {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    65
  if (path.length == dim * dim) 1
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    66
  else 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    67
    (for (x <- legal_moves(dim, path, path.head)) yield tcount_tours(dim, x::path)).sum
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    68
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    69
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    70
def count_tours(dim: Int, path: Path) =
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    71
  time_needed(tcount_tours(dim: Int, path: Path))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    72
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    73
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    74
def tenum_tours(dim: Int, path: Path): List[Path] = {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    75
  if (path.length == dim * dim) List(path)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    76
  else 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    77
    (for (x <- legal_moves(dim, path, path.head)) yield tenum_tours(dim, x::path)).flatten
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    78
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    79
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    80
def enum_tours(dim: Int, path: Path) =
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    81
  time_needed(tenum_tours(dim: Int, path: Path))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    82
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    83
// test cases
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    84
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    85
/*
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    86
def count_all_tours(dim: Int) = {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    87
  for (i <- (0 until dim).toList; 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    88
       j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    89
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    90
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    91
def enum_all_tours(dim: Int): List[Path] = {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    92
  (for (i <- (0 until dim).toList; 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    93
        j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    94
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    95
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    96
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    97
println("Number of tours starting from (0, 0)")
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    98
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    99
for (dim <- 1 to 5) {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   100
  println(s"${dim} x ${dim} " + time_needed(0, count_tours(dim, List((0, 0)))))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   101
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   102
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   103
println("Number of tours starting from all fields")
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   104
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   105
for (dim <- 1 to 5) {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   106
  println(s"${dim} x ${dim} " + time_needed(0, count_all_tours(dim)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   107
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   108
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   109
for (dim <- 1 to 5) {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   110
  val ts = enum_tours(dim, List((0, 0)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   111
  println(s"${dim} x ${dim} ")   
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   112
  if (ts != Nil) {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   113
    print_board(dim, ts.head)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   114
    println(ts.head)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   115
  }
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   116
}
220
1c47cede8e71 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
*/
247
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   118
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   119
// 1 mark
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   120
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   121
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   122
  case Nil => None
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   123
  case x::xs => {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   124
    val result = f(x)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   125
    if (result.isDefined) result else first(xs, f)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   126
  }
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   127
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   128
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   129
// test cases
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   130
//def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   131
//
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   132
//first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   133
//first(List((1, 0),(2, 0),(3, 0)), foo)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   134
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   135
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   136
// 1 mark
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   137
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   138
def tfirst_tour(dim: Int, path: Path): Option[Path] = {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   139
  if (path.length == dim * dim) Some(path)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   140
  else
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   141
    first(legal_moves(dim, path, path.head), (x:Pos) => tfirst_tour(dim, x::path))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   142
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   143
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   144
def first_tour(dim: Int, path: Path) = 
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   145
  time_needed(tfirst_tour(dim: Int, path: Path))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   146
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   147
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   148
/*
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   149
for (dim <- 1 to 8) {
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   150
  val t = first_tour(dim, List((0, 0)))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   151
  println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   152
}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   153
*/
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   154
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   155
// 15 secs for 8 x 8
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   156
//val ts1 = time_needed(0,first_tour(8, List((0, 0))).get)
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   157
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   158
// no result for 4 x 4
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   159
//val ts2 = time_needed(0, first_tour(4, List((0, 0))))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   160
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   161
// 0.3 secs for 6 x 6
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   162
//val ts3 = time_needed(0, first_tour(6, List((0, 0))))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   163
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   164
// 15 secs for 8 x 8
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   165
//time_needed(0, print_board(8, first_tour(8, List((0, 0))).get))
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   166
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   167
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   168
//}
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   169
87047208d5f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   170