progs/knight2_sol.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 14 Nov 2016 03:25:14 +0000
changeset 45 8399976b77fe
child 50 9891c9fac37e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 about finding a single tour for a board
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//================================================
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
type Pos = (Int, Int)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Path = List[Pos]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def print_board(dim: Int, path: Path): Unit = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  for (i <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
    for (j <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  } 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def add_pair(x: Pos)(y: Pos): Pos = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  (x._1 + y._1, x._2 + y._2)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def moves(x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  moves(x).filter(is_legal(dim, path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  case Nil => None
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  case x::xs => {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    val result = f(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    if (result.isDefined) result else first(xs, f)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
def first_tour(dim: Int, path: Path): Option[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  if (path.length == dim * dim) Some(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  else
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
for (dim <- 1 to 8) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  val t = first_tour(dim, List((0, 0)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51