progs/knight2.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 02 Nov 2016 13:26:26 +0000
changeset 5 c1e6123d02f4
parent 4 f31c22f4f104
child 6 aae256985251
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
5
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    27
def ordered_moves(n: Int)(steps: List[Pos])(x : Pos): List[Pos] = 
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    28
  moves(n)(x).sortBy((x: Pos) => moves(n)(x).filterNot(steps.contains(_)).length)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
moves(8)(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
ordered_moves(8)(Nil)(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
5
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    34
// non-circle tour
c1e6123d02f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    35
def tour(n: Int)(steps: List[Pos]): Unit = {
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  if (steps.length ==  n * n)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    print_board(n)(steps)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  else 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
val n = 8
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
println(s"simple tour: n = $n")
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
for (i <- 0 until n; j <- 0 until n) tour(n)(List((i, j)))