-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknight2.scala
74 lines (57 loc) · 2.25 KB
/
knight2.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Core Part about finding a single tour for a board using the
// Warnsdorf Rule
//==============================================================
object M4b {
// !!! Copy any function you need from file knight1.scala !!!
//
// If you need any auxiliary functions, feel free to
// implement them, but do not make any changes to the
// templates below.
type Pos = (Int, Int) // a position on a chessboard
type Path = List[Pos] // a path...a list of positions
// ADD YOUR CODE BELOW
//======================
import scala.annotation.tailrec
//(6)
//(1)
def is_legal(dim: Int, path: Path, x: Pos) : Boolean = {
if ((x._1 >= dim || x._2 >= dim || x._1 < 0 || x._2 < 0) || path.contains(x)) false else true
}
//(2)
def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
val moves = List((1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1),(-2,1),(-1,2)) //stores the list for all possible moves
(for (i <- 0 to moves.size-1 if(is_legal(dim,path,(x._1+moves(i)._1,x._2+moves(i)._2)))) yield (x._1+moves(i)._1,x._2+moves(i)._2)).toList
}
def ordered_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
val moves = legal_moves(dim,path,x)
moves.sortBy(pos => legal_moves(dim,path,pos).size)
}
//(7)
def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = xs match {
case Nil => None
case x :: tail => {
val proc = f(x)
if (proc != None) proc else first(tail,f)
}
}
def first_closed_tour_heuristics(dim: Int, path: Path) : Option[Path] = {
if(path.size == dim * dim && ordered_moves(dim,path.tail,path(path.size-1)).contains(path.head)){
Some(path)
}
else {
first(ordered_moves(dim,path,path.head),p=>first_closed_tour_heuristics(dim, p::path))
}
}
//(8) Same as (7) but searches for *non-closed* tours. This
// version of the function will be called with dimensions of
// up to 30 * 30.
def first_tour_heuristics(dim: Int, path: Path) : Option[Path] = {
if (path.size == dim * dim) Some(path)
else first(ordered_moves(dim,path,path.head),p=>first_tour_heuristics(dim,p::path))
}
}
// This template code is subject to copyright
// by King's College London, 2022. Do not
// make the template code public in any shape
// or form, and do not exchange it with other
// students under any circumstance.