progs/knight2.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 12 Nov 2016 15:20:56 +0000
changeset 39 c6fe374a5fca
parent 8 ab77f6006f1f
child 42 a5106bc13db6
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
5
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     4
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     5
def print_board(n: Int)(steps: List[Pos]): Unit = {
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  for (i <- 0 until n) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
    for (j <- 0 until n) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
      print(f"${steps.indexOf((i, j))}%3.0f ")
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
    }
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    println
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  } 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  //readLine()
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  System.exit(0)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
5
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    16
def add_pair(x: Pos)(y: Pos) = 
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  (x._1 + y._1, x._2 + y._2)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
5
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    19
def is_legal(n: Int)(x: Pos) = 
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
5
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    22
def moves(n: Int)(x: Pos): List[Pos] = {
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  List((1, 2),(2, 1),(2, -1),(1, -2),
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
       (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    27
// non-circle tours
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    28
def tour(n: Int)(steps: List[Pos]): List[List[Pos]] = {
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    29
  if (steps.length ==  n * n) // && moves(n)(steps.head).contains(steps.last)) 
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    30
    List(steps)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  else 
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    32
    (for (x <- moves(n)(steps.head).par; 
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    33
          if (!steps.contains(x))) yield tour(n)(x :: steps)).toList.flatten
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
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    40
println(tour(n)(List((1,1))).distinct.size)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    41
//println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size)