main_solution4/knight4.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 23:12:49 +0000
changeset 467 9b5165b8a762
parent 432 de701b64a4e0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
430
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     1
// Part 4 about finding a single tour on "mutilated" chessboards
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     2
//==============================================================
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     3
432
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
     4
object M4d { // for preparing the jar
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def print_board(dim: Int, path: Path): Unit = {
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 221
diff changeset
    10
  println()
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  for (i <- 0 until dim) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    for (j <- 0 until dim) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
      print(f"${path.reverse.indexOf((i, j))}%4.0f ")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    }
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 221
diff changeset
    15
    println()
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  } 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
432
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    19
def add_pair(x: Pos, y: Pos): Pos = 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    20
  (x._1 + y._1, x._2 + y._2)
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    21
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    22
def is_legal(dim: Int, path: Path, x: Pos): Boolean = 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    23
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    24
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    25
def moves(x: Pos): List[Pos] = 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    26
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    27
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x, _))
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    28
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    29
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    30
  moves(x).filter(is_legal(dim, path, _))
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    31
 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    32
import scala.annotation.tailrec
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
432
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    34
@tailrec
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    35
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    36
  case Nil => None
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    37
  case x::xs => {
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    38
    val result = f(x)
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    39
    if (result.isDefined) result else first(xs, f)
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    40
  }
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    41
}
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    42
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    43
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    44
def one_tour_pred(dim: Int, path: Path, n: Int, pred: Pos => Boolean): Option[Path] = {
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    45
  if (path.length == n) Some(path)
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    46
  else
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    47
    first(legal_moves(dim, path, path.head).filter(pred), (x: Pos) => one_tour_pred(dim, x::path, n, pred))
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    48
}
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    49
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    50
//print_board(8, one_tour_pred(8, List((0, 0)), 40, x => x._1 < 5).get)
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
}