author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Tue, 15 Nov 2016 23:08:09 +0000 | |
changeset 49 | fdc2c6fb7a24 |
parent 44 | 9cfa37c91444 |
child 50 | 9891c9fac37e |
permissions | -rw-r--r-- |
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
1 |
|
5 | 2 |
type Pos = (Int, Int) |
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
3 |
type Path = List[Pos] |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
4 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
5 |
def print_board(dim: Int, path: Path): Unit = { |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
6 |
println |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
7 |
for (i <- 0 until dim) { |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
8 |
for (j <- 0 until dim) { |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
9 |
print(f"${path.indexOf((i, j))}%3.0f ") |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
10 |
} |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
11 |
println |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
12 |
} |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
13 |
} |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
14 |
|
4 | 15 |
|
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
16 |
def add_pair(x: Pos)(y: Pos): Pos = |
4 | 17 |
(x._1 + y._1, x._2 + y._2) |
18 |
||
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
19 |
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
20 |
0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) |
4 | 21 |
|
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
22 |
def moves(x: Pos): List[Pos] = { |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
23 |
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
|
24 |
(-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
|
25 |
} |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
26 |
|
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
27 |
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
28 |
moves(x).filter(is_legal(dim, path)) |
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
29 |
|
4 | 30 |
|
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
31 |
|
6 | 32 |
// non-circle tours |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
33 |
/* |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
List(path) |
4 | 37 |
else |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
38 |
(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
|
39 |
} |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
40 |
*/ |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
41 |
|
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
42 |
def tour(dim: Int, path: Path): Int = { |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
43 |
if (path.length == dim * dim) 1 |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
44 |
else |
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
45 |
(for (x <- legal_moves(dim, path, path.head) yield tour(dim, x::path))).sum |
4 | 46 |
} |
47 |
||
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
48 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
49 |
def dtour(dim: Int): List[List[Pos]] = { |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
50 |
var counter = 100000000 |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
51 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
52 |
def etour(dim: Int, path: List[Pos]): List[List[Pos]] = { |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
53 |
counter = counter - 1 |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
54 |
if (counter <= 0) List() else |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
55 |
if (path.length == dim * dim) List(path) |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
56 |
else |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
57 |
(for (x <- legal_moves(dim, path, path.head)) yield etour(dim, x::path)).flatten |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
58 |
} |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
59 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
60 |
(for (i <- (0 until dim).toList; |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
61 |
j <- (0 until dim).toList) yield etour(dim, List((i, j)))).flatten |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
62 |
} |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
63 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
64 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
65 |
|
6 | 66 |
//val n = 8 |
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
67 |
val n = 5 |
6 | 68 |
println(s"number simple tours: n = $n") |
4 | 69 |
|
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
70 |
//println(etour(n, List((0, 0))).size) |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
71 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
72 |
|
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
73 |
|
44
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
74 |
for (d <- 9 to 9) { |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
75 |
println(s"${d} x ${d} " + dtour(d).length) |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
76 |
} |
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
77 |
|
9cfa37c91444
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
43
diff
changeset
|
78 |