progs/knight2.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 13 Nov 2016 11:56:06 +0000
changeset 43 ba4081c70de4
parent 42 a5106bc13db6
child 44 9cfa37c91444
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import scala.util._
5
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     2
type Pos = (Int, Int)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
     4
def add_pair(x: Pos)(y: Pos): Pos = 
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
  (x._1 + y._1, x._2 + y._2)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
     7
def is_legal(dim: Int, path: List[Pos])(x: Pos): Boolean = 
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
     8
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    10
def moves(x: Pos): List[Pos] = {
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    11
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    12
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    13
}
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    14
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    15
def legal_moves(dim: Int, path: List[Pos], x: Pos): List[Pos] = {
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    16
  moves(x).filter(is_legal(dim, path))
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    19
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    20
// non-circle tours
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    21
/*
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    22
def tour(dim: Int, path: List[Pos]): List[List[Pos]] = {
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    23
  if (path.length ==  dim * dim) // && moves(n)(path.head).contains(path.last)) 
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    24
    List(path)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  else 
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    26
    (for (x <- legal_moves(dim, path, path.head)) yield tour(dim, x::path)).flatten
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    27
}
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    28
*/
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    29
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    30
def tour(dim: Int, path: List[Pos]): Int = {
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    31
  if (path.length == dim * dim) 1
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    32
  else 
43
ba4081c70de4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 42
diff changeset
    33
    (for (x <- legal_moves(dim, path, path.head).par) yield tour(dim, x::path)).sum
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    36
//val n = 8
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    37
val n = 6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    38
println(s"number simple tours: n = $n")
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
43
ba4081c70de4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 42
diff changeset
    40
println(tour(n, List((0, 0))))
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    41
43
ba4081c70de4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 42
diff changeset
    42
/*
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    43
for (d <- 1 to 6) {
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    44
  println(s"${d} x ${d} " + (for (i <- 0 until d; j <- 0 until d) yield tour(d, List((i, j)))).sum)
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    45
} 
43
ba4081c70de4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 42
diff changeset
    46
*/