progs/knight2_sol.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 21 Nov 2016 19:23:37 +0000
changeset 64 d6f97b562424
parent 50 9891c9fac37e
child 66 3506b681c191
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
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
     4
type Pos = (Int, Int)    // a position on a chessboard 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
     5
type Path = List[Pos]    // a path...a list of positions
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def print_board(dim: Int, path: Path): Unit = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  for (i <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    for (j <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  } 
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
def add_pair(x: Pos)(y: Pos): Pos = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  (x._1 + y._1, x._2 + y._2)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  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
    22
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def moves(x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  moves(x).filter(is_legal(dim, path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  case Nil => None
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  case x::xs => {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    val result = f(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    if (result.isDefined) result else first(xs, f)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  }
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
def first_tour(dim: Int, path: Path): Option[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  if (path.length == dim * dim) Some(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  else
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
    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
    44
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
for (dim <- 1 to 8) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  val t = first_tour(dim, List((0, 0)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  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
    49
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
 
64
d6f97b562424 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    51
d6f97b562424 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    52
d6f97b562424 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    53