progs/knight3_sol.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 16 Nov 2016 23:34:05 +0000
changeset 53 9f8751912560
parent 50 9891c9fac37e
child 66 3506b681c191
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: 45
diff changeset
     1
// Part 3 about finding a single tour using the Warnsdorf Rule
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
     2
//=============================================================
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     4
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     5
type Pos = (Int, Int)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     6
type Path = List[Pos]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     7
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     8
def print_board(dim: Int, path: Path): Unit = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     9
  println
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    10
  for (i <- 0 until dim) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    11
    for (j <- 0 until dim) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    12
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    13
    }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    14
    println
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    15
  } 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    16
}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    18
def add_pair(x: Pos)(y: Pos): Pos = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    19
  (x._1 + y._1, x._2 + y._2)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    20
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    21
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    22
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    24
def moves(x: Pos): List[Pos] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    25
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    26
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    27
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    28
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    29
  moves(x).filter(is_legal(dim, path))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    30
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    31
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    32
  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    33
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    35
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    36
  case Nil => None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    37
  case x::xs => {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    38
    val result = f(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    39
    if (result.isDefined) result else first(xs, f)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    40
  }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    41
}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    43
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    44
def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    45
  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    46
  else
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    47
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    48
}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    50
for (dim <- 1 to 6) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    51
  val t = first_closed_tour_heuristics(dim, List((dim / 2, dim / 2)))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    52
  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    53
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    54
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    56
def first_tour_heuristics(dim: Int, path: Path): Option[Path] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    57
  if (path.length == dim * dim) Some(path)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    58
  else
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    59
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    60
}
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    61
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    62
for (dim <- 1 to 50) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    63
  val t = first_tour_heuristics(dim, List((dim / 2, dim / 2)))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    64
  println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    65
}