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