1 // Part 2 about finding a single tour for a board |
1 // Part 2 about finding a single tour for a board |
2 //================================================ |
2 //================================================ |
3 |
|
4 // copy any function you need from file knight1.scala |
|
5 |
3 |
6 type Pos = (Int, Int) // a position on a chessboard |
4 type Pos = (Int, Int) // a position on a chessboard |
7 type Path = List[Pos] // a path...a list of positions |
5 type Path = List[Pos] // a path...a list of positions |
8 |
6 |
9 |
7 |
10 //(2a) Implement a first-function that finds the first |
8 // for measuring time |
11 // element, say x, in the list xs where f is not None. |
9 def time_needed[T](n: Int, code: => T) : T = { |
12 // In that case return f(x), otherwise none. |
10 val start = System.nanoTime() |
|
11 for (i <- 0 until n) code |
|
12 val result = code |
|
13 val end = System.nanoTime() |
|
14 println("Time needed: " + (end - start) / 1.0e9 + " secs.") |
|
15 result |
|
16 } |
13 |
17 |
14 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = ... |
|
15 |
18 |
16 //(2b) Implement a function that uses the first-function for |
19 def print_board(dim: Int, path: Path): Unit = { |
17 // trying out onward moves, and searches recursively for an |
20 println |
18 // *open* tour on a dim * dim-board. |
21 for (i <- 0 until dim) { |
|
22 for (j <- 0 until dim) { |
|
23 print(f"${path.reverse.indexOf((i, j))}%3.0f ") |
|
24 } |
|
25 println |
|
26 } |
|
27 } |
19 |
28 |
20 def first_tour(dim: Int, path: Path): Option[Path] = ... |
29 def add_pair(x: Pos)(y: Pos): Pos = |
21 |
30 (x._1 + y._1, x._2 + y._2) |
|
31 |
|
32 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = |
|
33 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) |
|
34 |
|
35 def moves(x: Pos): List[Pos] = |
|
36 List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), |
|
37 (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)) |
|
38 |
|
39 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = |
|
40 moves(x).filter(is_legal(dim, path)) |
|
41 |
|
42 |
|
43 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { |
|
44 case Nil => None |
|
45 case x::xs => { |
|
46 val result = f(x) |
|
47 if (result.isDefined) result else first(xs, f) |
|
48 } |
|
49 } |
|
50 |
|
51 first(List((1, 0),(2, 0),(3, 0),(4, 0)), (x => if (x._1 > 3) Some(List(x)) else None)) |
|
52 first(List((1, 0),(2, 0),(3, 0)), (x => if (x._1 > 3) Some(List(x)) else None)) |
|
53 |
|
54 |
|
55 |
|
56 def first_tour(dim: Int, path: Path): Option[Path] = { |
|
57 if (path.length == dim * dim) Some(path) |
|
58 else |
|
59 first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path)) |
|
60 } |
|
61 |
|
62 /* |
|
63 for (dim <- 1 to 8) { |
|
64 val t = first_tour(dim, List((0, 0))) |
|
65 println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) |
|
66 } |
|
67 */ |
|
68 |
|
69 // 15 secs for 8 x 8 |
|
70 val ts1 = time_needed(0,first_tour(8, List((0, 0))).get) |
|
71 |
|
72 // no result for 4 x 4 |
|
73 val ts2 = time_needed(0, first_tour(4, List((0, 0)))) |
|
74 |
|
75 // 0.3 secs for 6 x 6 |
|
76 val ts3 = time_needed(0, first_tour(6, List((0, 0)))) |
|
77 |
|
78 // 15 secs for 8 x 8 |
|
79 time_needed(0, print_board(8, first_tour(8, List((0, 0))).get)) |
|
80 |
|
81 |