progs/knight6.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 24 Nov 2017 01:26:01 +0000
changeset 154 39c6b93718f0
parent 41 1fe8205f6bdb
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import scala.util._
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
type Pos = (Int, Int)
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
def print_board(n: Int)(steps: List[Pos]): Unit = {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
  for (i <- 0 until n) {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
    for (j <- 0 until n) {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
      print(f"${steps.reverse.indexOf((i, j))}%3.0f ")
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    }
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
    println
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  } 
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
}
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def add_pair(x: Pos)(y: Pos) = 
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  (x._1 + y._1, x._2 + y._2)
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def dist(n: Int)(y: Pos) = 
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  (n / 2 - y._1).abs + (n / 2 - y._2).abs
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def is_legal(n: Int)(x: Pos) = 
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def moves(n: Int)(x: Pos): List[Pos] = {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x)).filter(is_legal(n))
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
}
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
def moves_filtered(n: Int)(steps: List[Pos])(x: Pos): List[Pos] = {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  moves(n)(x).filterNot(steps.contains(_))
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def ordered_moves(n: Int)(steps: List[Pos])(x: Pos): List[Pos] = 
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  moves_filtered(n)(steps)(x).sortBy((x: Pos) => (moves_filtered(n)(steps)(x).length, dist(n)(x)))
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
/*def first[A, B](xs: List[A], f: A => Option[B]): Option[B] = xs match {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  case Nil => None
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case x::xs => {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    val result = f(x)
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    if (result.isDefined) result else first(xs, f)
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  }
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}*/
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
def first[A, B](xs: List[A], f: A => Option[B]): Option[B] =
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  xs.par.flatMap(f(_)).headOption
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// non-circle tours, including distance
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
def tour(n: Int)(steps: List[Pos]): Option[List[Pos]] = {
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  if (steps.length ==  n * n) //&& moves(n)(steps.head).contains(steps.last)) 
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    Some(steps)
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  else first(ordered_moves(n)(steps)(steps.head), (x: Pos) => tour(n)(x::steps))
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
}
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
//val n = 8
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
val n = 6
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
println(s"number simple tours: n = $n")
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
println(print_board(n)((tour(n)(List((0, 0)))).get))
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
//println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size) 
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
/*
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
def first[A, B](xs: List[A], f: A => Option[B]): Option[B] =
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  xs.view.flatMap(f(_)).headOption
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
*/
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
/*
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
1fe8205f6bdb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
*/