progs/knight2.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 03 Nov 2016 00:53:53 +0000
changeset 6 aae256985251
parent 5 c1e6123d02f4
child 7 7a5a29a32568
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]] = {
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    29
  if (steps.length ==  n * n) List(steps)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  else 
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    31
    (for (x <- moves(n)(steps.head); if (!steps.contains(x))) yield tour(n)(x :: steps)).flatten
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    34
//val n = 8
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    35
val n = 6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    36
println(s"number simple tours: n = $n")
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    38
println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size)