Attic/knight.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 14 Jul 2024 14:31:39 +0100
changeset 492 4ffba2f72692
parent 468 0587ef444547
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
     1
import scala.util._
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
def print_board(n: Int)(steps: List[(Int, Int)]): Unit = {
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
  for (i <- 0 until n) {
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
    for (j <- 0 until n) {
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
     6
      print(f"${steps.indexOf((i, j))}%3.0f ")
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
    }
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
    println
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  } 
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
  //readLine()
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    11
  //System.exit(0)
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    12
  throw new Exception("\n")
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
}
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
def add_pair(x: (Int, Int))(y: (Int, Int)) = 
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  (x._1 + y._1, x._2 + y._2)
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
def is_legal(n: Int)(x: (Int, Int)) = 
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = {
1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 0
diff changeset
    22
  List((1, 2),(2, 1),(2, -1),(1, -2),
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 0
diff changeset
    23
       (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
}
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    26
def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = 
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    27
  moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length)
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    28
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    29
moves(8)(1,3)
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    30
ordered_moves(8)(Nil)(1,3)
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    31
ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    32
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    33
// non-circle tour parallel
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
def tour(n: Int)(steps: List[(Int, Int)]): Unit = {
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    35
  if (steps.length ==  n * n)
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
    print_board(n)(steps)
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  else 
2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 1
diff changeset
    38
    for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps)
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
}
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    41
// non-circle tour
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    42
def ctour(n: Int)(steps: List[(Int, Int)]): Unit = {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    43
  if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    44
    print_board(n)(steps)
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    45
  else 
9
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    46
    for (x <- moves(n)(steps.head); if (!steps.contains(x))) ctour(n)(x :: steps)
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    47
}
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    48
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    49
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    50
def faster_tour(n: Int)(steps: List[(Int, Int)]): Unit = {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    51
  if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    52
    print_board(n)(steps)
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    53
  else 
9
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    54
    for (x <- ordered_moves(n)(steps)(steps.head); if (!steps.contains(x))) faster_tour(n)(x :: steps)
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    55
}
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    56
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    57
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    58
val n1 = 5
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    59
println(s"simple tour: n = $n1")
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    61
Try {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    62
  for (i <- 0 until n1; j <- 0 until n1) {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    63
    tour(n1)(List((i, j))) 
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    64
  }
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    65
}
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    66
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    67
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    68
val n2 = 6
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    69
println(s"circle tour: n = $n2")
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    70
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    71
Try {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    72
  for (i <- 0 until n2; j <- 0 until n2) {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    73
    ctour(n2)(List((i, j))) 
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    74
  }
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    75
}
9
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    76
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    77
9
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    78
val n3 = 8
3
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    79
println(s"fast circle tour: n = $n3")
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    80
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    81
Try {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    82
  for (i <- 0 until n3; j <- 0 until n3) {
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    83
    faster_tour(n3)(List((i, j))) 
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    84
  }
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    85
}
575a5c07c7fe updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 2
diff changeset
    86
0
02f53f76f828 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
println("finished")