progs/knight1_sol.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 27 Jan 2017 14:55:56 +0000
changeset 109 293ea84d82ca
parent 86 f8a781322499
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about finding and counting Knight's tours
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================================
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
type Pos = (Int, Int)    // a position on a chessboard 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
type Path = List[Pos]    // a path...a list of positions
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def print_board(dim: Int, path: Path): Unit = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  for (i <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    for (j <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  } 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def add_pair(x: Pos)(y: Pos): Pos = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  (x._1 + y._1, x._2 + y._2)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    23
assert(is_legal(8, Nil)((3,4)) == true)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    24
assert(is_legal(8, List((4,1), (1,0)))((4,1)) == false)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    25
assert(is_legal(2, Nil)((0,0)) == true)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    26
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
def moves(x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  moves(x).filter(is_legal(dim, path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    34
assert(legal_moves(8, Nil, (2,2)) == 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    35
  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    36
assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    37
assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    38
  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    39
assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
85
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    40
assert(legal_moves(1, Nil, (0,0)) == List())
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    41
assert(legal_moves(2, Nil, (0,0)) == List())
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    42
assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    43
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
def count_tours(dim: Int, path: Path): Int = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  if (path.length == dim * dim) 1
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
def enum_tours(dim: Int, path: Path): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  if (path.length == dim * dim) List(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
    (for (x <- legal_moves(dim, path, path.head)) yield enum_tours(dim, x::path)).flatten
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    57
def count_all_tours(dim: Int) = {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    58
  for (i <- (0 until dim).toList; 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    59
       j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def enum_all_tours(dim: Int): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  (for (i <- (0 until dim).toList; 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
        j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    67
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    68
def add_pair_urban(x: Pos)(y: Pos): Pos = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    69
  (x._1 + y._1, x._2 + y._2)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    70
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    71
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    72
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    73
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    74
def moves_urban(x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    75
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    76
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    77
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    78
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    79
  moves_urban(x).filter(is_legal_urban(dim, path))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    80
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    81
def correct_urban(dim: Int)(p: Path): Boolean = p match {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    82
  case Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    83
  case x::Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    84
  case x::y::p => if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    85
}
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    87
enum_tours(5, List((0, 2))).map(correct_urban(5)).forall(_ == true)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    88
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    89
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  println(s"${dim} x ${dim} " + count_tours(dim, List((0, 0))))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  println(s"${dim} x ${dim} " + count_all_tours(dim))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  val ts = enum_tours(dim, List((0, 0)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  println(s"${dim} x ${dim} ")   
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  if (ts != Nil) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    print_board(dim, ts.head)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
    println(ts.head)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107