marking2/knight3.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 16 Jun 2018 00:07:32 +0100
changeset 182 d3d912d7e17f
parent 168 03530cb87cd0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
168
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 3 about finding a single tour using the Warnsdorf Rule
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//=============================================================
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW7c {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def print_board(dim: Int, path: Path): Unit = {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  println
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  for (i <- 0 until dim) {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    for (j <- 0 until dim) {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    }
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    println
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  } 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def add_pair(x: Pos)(y: Pos): Pos = 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  (x._1 + y._1, x._2 + y._2)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def moves(x: Pos): List[Pos] = 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  moves(x).filter(is_legal(dim, path))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
import scala.annotation.tailrec
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
@tailrec
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  case Nil => None
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case x::xs => {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
    val result = f(x)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
    if (result.isDefined) result else first(xs, f)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  }
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
}
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
def first_closed_tour_heuristic(dim: Int, path: Path): Option[Path] = {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  else
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristic(dim, x::path))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
}
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
/*
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
for (dim <- 1 to 6) {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  val t = first_closed_tour_heuristic(dim, List((dim / 2, dim / 2)))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
}*/
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
def first_tour_heuristic(dim: Int, path: Path): Option[Path] = {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  @tailrec
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  def aux(dim: Int, path: Path, moves: List[Pos]): Option[Path] =  
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  if (path.length == dim * dim) Some(path)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  else
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
    moves match {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
      case Nil => None
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
      case x::xs => {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
        val r = first_tour_heuristic(dim, x::path)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
        if (r.isDefined) r else aux(dim, path, xs)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
    }
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  }    
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  aux(dim, path, ordered_moves(dim, path, path.head)) 
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
}
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
/*
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
def first_tour_heuristic(dim: Int, path: Path): Option[Path] = {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  if (path.length == dim * dim) Some(path)
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  else
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristic(dim, x::path))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
}
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
*/
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
/*
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
for (dim <- 1 to 50) {
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  val t = first_tour_heuristic(dim, List((dim / 2, dim / 2)))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
}
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
*/
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
}
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
03530cb87cd0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
//CW7c.first_tour_heuristic(50, List((0,0))).get