testing3-bak/knight3.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 06 Dec 2018 13:15:28 +0000
changeset 229 5549016ab10f
parent 216 8c868feb917b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     1
// Part 3 about finding a single tour using the Warnsdorf Rule
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     2
//=============================================================
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     3
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
     4
//object CW8c { // for preparing the jar
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     5
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     6
type Pos = (Int, Int)
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     7
type Path = List[Pos]
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
     9
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    10
// for measuring time in the JAR
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    11
def time_needed[T](code: => T) : T = {
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    12
  val start = System.nanoTime()
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    13
  val result = code
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    14
  val end = System.nanoTime()
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    15
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    16
  result
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    17
}
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    18
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    19
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    20
def print_board(dim: Int, path: Path): Unit = {
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    21
  println
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    22
  for (i <- 0 until dim) {
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    23
    for (j <- 0 until dim) {
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    24
      print(f"${path.reverse.indexOf((i, j))}%4.0f ")
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    25
    }
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    26
    println
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    27
  } 
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    30
def add_pair(x: Pos, y: Pos): Pos = 
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    31
  (x._1 + y._1, x._2 + y._2)
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    32
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    33
def is_legal(dim: Int, path: Path, x: Pos): Boolean = 
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    34
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    35
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    36
def moves(x: Pos): List[Pos] = 
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    37
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    38
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x, _))
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    39
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    40
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    41
  moves(x).filter(is_legal(dim, path, _))
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    42
 
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    43
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    44
  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    45
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    46
import scala.annotation.tailrec
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    47
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    48
@tailrec
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    49
def tour_on_mega_board_aux(dim: Int, paths: List[Path]): Option[Path] = paths match {
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    50
  case Nil => None
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    51
  case (path::rest) =>
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    52
    if (path.length == dim * dim) Some(path)
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    53
    else tour_on_mega_board_aux(dim, ordered_moves(dim, path, path.head).map(_::path) ::: rest)
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    56
def ttour_on_mega_board(dim: Int, path: Path): Option[Path] =
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    57
  tour_on_mega_board_aux(dim, List(path))
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    58
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    59
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    60
def tour_on_mega_board(dim: Int, path: Path) =
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    61
  time_needed(ttour_on_mega_board(dim: Int, path: Path))
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    62
216
8c868feb917b updated
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    63
//}