3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
1 |
import scala.util._
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
def print_board(n: Int)(steps: List[(Int, Int)]): Unit = {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
for (i <- 0 until n) {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
for (j <- 0 until n) {
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
6 |
print(f"${steps.indexOf((i, j))}%3.0f ")
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
println
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
//readLine()
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
11 |
//System.exit(0)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
12 |
throw new Exception("\n")
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
def add_pair(x: (Int, Int))(y: (Int, Int)) =
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
(x._1 + y._1, x._2 + y._2)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
def is_legal(n: Int)(x: (Int, Int)) =
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = {
|
1
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
22 |
List((1, 2),(2, 1),(2, -1),(1, -2),
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
23 |
(-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
26 |
def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] =
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
27 |
moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
28 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
29 |
moves(8)(1,3)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
30 |
ordered_moves(8)(Nil)(1,3)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
31 |
ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
32 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
33 |
// non-circle tour parallel
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
def tour(n: Int)(steps: List[(Int, Int)]): Unit = {
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
35 |
if (steps.length == n * n)
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
print_board(n)(steps)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
else
|
2
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
38 |
for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps)
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
41 |
// non-circle tour
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
42 |
def ctour(n: Int)(steps: List[(Int, Int)]): Unit = {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
43 |
if (steps.length == n * n && moves(n)(steps.head).contains(steps.last))
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
44 |
print_board(n)(steps)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
45 |
else
|
9
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
46 |
for (x <- moves(n)(steps.head); if (!steps.contains(x))) ctour(n)(x :: steps)
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
47 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
48 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
49 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
50 |
def faster_tour(n: Int)(steps: List[(Int, Int)]): Unit = {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
51 |
if (steps.length == n * n && moves(n)(steps.head).contains(steps.last))
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
52 |
print_board(n)(steps)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
53 |
else
|
9
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
54 |
for (x <- ordered_moves(n)(steps)(steps.head); if (!steps.contains(x))) faster_tour(n)(x :: steps)
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
55 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
56 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
57 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
58 |
val n1 = 5
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
59 |
println(s"simple tour: n = $n1")
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
61 |
Try {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
62 |
for (i <- 0 until n1; j <- 0 until n1) {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
63 |
tour(n1)(List((i, j)))
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
64 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
65 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
66 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
67 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
68 |
val n2 = 6
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
69 |
println(s"circle tour: n = $n2")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
70 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
71 |
Try {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
72 |
for (i <- 0 until n2; j <- 0 until n2) {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
73 |
ctour(n2)(List((i, j)))
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
74 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
75 |
}
|
9
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
76 |
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
77 |
|
9
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
78 |
val n3 = 8
|
3
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
79 |
println(s"fast circle tour: n = $n3")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
80 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
81 |
Try {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
82 |
for (i <- 0 until n3; j <- 0 until n3) {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
83 |
faster_tour(n3)(List((i, j)))
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
84 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
85 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
86 |
|
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
println("finished")
|