progs/knight2.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 29 Oct 2019 14:34:51 +0000
changeset 282 ec9773fe1dc0
parent 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: 44
diff changeset
     1
// Part 2 about finding a single tour for a board
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 44
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: 44
diff changeset
     4
type Pos = (Int, Int)    // a position on a chessboard 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 44
diff changeset
     5
type Path = List[Pos]    // a path...a list of positions
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
     7
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     8
// for measuring time
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     9
def time_needed[T](n: Int, code: => T) : T = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    10
  val start = System.nanoTime()
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    11
  for (i <- 0 until n) code
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    12
  val result = code
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    13
  val end = System.nanoTime()
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    14
  println("Time needed: " + (end - start) / 1.0e9 + " secs.")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    15
  result
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    16
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    17
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    18
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    19
def print_board(dim: Int, path: Path): Unit = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    20
  println
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    21
  for (i <- 0 until dim) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    22
    for (j <- 0 until dim) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    23
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    24
    }
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    25
    println
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    26
  } 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    27
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    28
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    29
def add_pair(x: Pos)(y: Pos): Pos = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    30
  (x._1 + y._1, x._2 + y._2)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    31
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    32
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    33
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    34
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    35
def moves(x: Pos): List[Pos] = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    36
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    37
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    38
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    39
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    40
  moves(x).filter(is_legal(dim, path))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    41
44
9cfa37c91444 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
    42
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    43
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    44
  case Nil => None
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    45
  case x::xs => {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    46
    val result = f(x)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    47
    if (result.isDefined) result else first(xs, f)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    48
  }
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    49
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    50
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    51
first(List((1, 0),(2, 0),(3, 0),(4, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    52
first(List((1, 0),(2, 0),(3, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    53
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    54
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    55
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    56
def first_tour(dim: Int, path: Path): Option[Path] = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    57
  if (path.length == dim * dim) Some(path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    58
  else
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    59
    first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    60
}
44
9cfa37c91444 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
    61
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    62
/*
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    63
for (dim <- 1 to 8) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    64
  val t = first_tour(dim, List((0, 0)))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    65
  println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    66
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    67
*/
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    68
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    69
// 15 secs for 8 x 8
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    70
val ts1 = time_needed(0,first_tour(8, List((0, 0))).get)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    71
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    72
// no result for 4 x 4
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    73
val ts2 = time_needed(0, first_tour(4, List((0, 0))))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    74
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    75
// 0.3 secs for 6 x 6
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    76
val ts3 = time_needed(0, first_tour(6, List((0, 0))))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    77
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    78
// 15 secs for 8 x 8
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    79
time_needed(0, print_board(8, first_tour(8, List((0, 0))).get))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    80
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    81