progs/knight3.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 02 Nov 2016 13:22:40 +0000
changeset 4 f31c22f4f104
child 50 9891c9fac37e
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._
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
def print_board(n: Int)(steps: List[(Int, Int)]): Unit = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
  for (i <- 0 until n) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
    for (j <- 0 until n) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
      print(f"${steps.indexOf((i, j))}%3.0f ")
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
    }
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
    println
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  } 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  //readLine()
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  System.exit(0)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def add_pair(x: (Int, Int))(y: (Int, Int)) = 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  (x._1 + y._1, x._2 + y._2)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def is_legal(n: Int)(x: (Int, Int)) = 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  List((1, 2),(2, 1),(2, -1),(1, -2),
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
       (-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
    23
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
moves(8)(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
ordered_moves(8)(Nil)(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// non-circle tour parallel
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def tour(n: Int)(steps: List[(Int, Int)]): Unit = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    print_board(n)(steps)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  else 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) tour(n)(x :: steps)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
val n = 7
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
println(s"circle tour parallel: n = $n")
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
val starts = for (i <- 0 until n; j <- 0 until n) yield (i, j)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
starts.par.foreach((x:(Int, Int)) => tour(n)(List(x)))