author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sun, 13 Nov 2016 01:27:32 +0000 | |
changeset 42 | a5106bc13db6 |
parent 39 | c6fe374a5fca |
child 43 | ba4081c70de4 |
permissions | -rw-r--r-- |
4 | 1 |
import scala.util._ |
5 | 2 |
type Pos = (Int, Int) |
4 | 3 |
|
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
4 |
def add_pair(x: Pos)(y: Pos): Pos = |
4 | 5 |
(x._1 + y._1, x._2 + y._2) |
6 |
||
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
7 |
def is_legal(dim: Int, path: List[Pos])(x: Pos): Boolean = |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
8 |
0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) |
4 | 9 |
|
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
10 |
def moves(x: Pos): List[Pos] = { |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
11 |
List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
12 |
(-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)) |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
13 |
} |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
14 |
|
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
15 |
def legal_moves(dim: Int, path: List[Pos], x: Pos): List[Pos] = { |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
16 |
moves(x).filter(is_legal(dim, path)) |
4 | 17 |
} |
18 |
||
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
19 |
|
6 | 20 |
// non-circle tours |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
21 |
/* |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
22 |
def tour(dim: Int, path: List[Pos]): List[List[Pos]] = { |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
23 |
if (path.length == dim * dim) // && moves(n)(path.head).contains(path.last)) |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
24 |
List(path) |
4 | 25 |
else |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
26 |
(for (x <- legal_moves(dim, path, path.head)) yield tour(dim, x::path)).flatten |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
27 |
} |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
28 |
*/ |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
29 |
|
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
30 |
def tour(dim: Int, path: List[Pos]): Int = { |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
31 |
if (path.length == dim * dim) 1 |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
32 |
else |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
33 |
(for (x <- legal_moves(dim, path, path.head)) yield tour(dim, x::path)).sum |
4 | 34 |
} |
35 |
||
6 | 36 |
//val n = 8 |
37 |
val n = 6 |
|
38 |
println(s"number simple tours: n = $n") |
|
4 | 39 |
|
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
40 |
//println(tour(n, List((0, 0)))) |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
41 |
|
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
42 |
for (d <- 1 to 6) { |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
43 |
println(s"${d} x ${d} " + (for (i <- 0 until d; j <- 0 until d) yield tour(d, List((i, j)))).sum) |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
44 |
} |