author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Fri, 16 Dec 2016 13:34:33 +0000 | |
changeset 88 | 990a1531135d |
parent 87 | 9384b8c98014 |
child 93 | 21f41e08457d |
permissions | -rw-r--r-- |
86 | 1 |
|
2 |
import scala.concurrent._ |
|
3 |
import scala.concurrent.duration._ |
|
4 |
import ExecutionContext.Implicits.global |
|
5 |
import scala.language.postfixOps |
|
6 |
||
7 |
||
8 |
def add_pair_urban(x: Pos)(y: Pos): Pos = |
|
9 |
(x._1 + y._1, x._2 + y._2) |
|
10 |
||
11 |
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = |
|
12 |
0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) |
|
13 |
||
14 |
def moves_urban(x: Pos): List[Pos] = |
|
15 |
List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), |
|
16 |
(-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair_urban(x)) |
|
17 |
||
18 |
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = |
|
19 |
moves_urban(x).filter(is_legal_urban(dim, path)) |
|
20 |
||
21 |
def correct_urban(dim: Int)(p: Path): Boolean = p match { |
|
22 |
case Nil => true |
|
23 |
case x::Nil => true |
|
24 |
case x::y::p => |
|
25 |
if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false |
|
26 |
} |
|
27 |
||
87
9384b8c98014
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
28 |
def correct_closed_urban(dim: Int)(path: Path): Boolean = |
9384b8c98014
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
29 |
correct_urban(dim)(path) && moves_urban(path.head).contains(path.last) |
9384b8c98014
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
30 |
|
86 | 31 |
|
32 |
lazy val f = Future { |
|
33 |
||
88
990a1531135d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
87
diff
changeset
|
34 |
val tsc = first_closed_tour_heuristic(6, List((3, 3))).get |
87
9384b8c98014
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
35 |
assert(correct_closed_urban(6)(tsc) == true) |
86 | 36 |
|
37 |
} |
|
38 |
||
39 |
Await.result(f, 300 second) |