1 // Part 1 about finding and counting Knight's tours |
1 // Main Part 4 about finding Knight's tours |
2 //================================================== |
2 //========================================== |
|
3 import scala.annotation.tailrec |
3 |
4 |
4 object CW9a { // for preparing the jar |
5 |
|
6 object M4a { |
|
7 |
|
8 // If you need any auxiliary functions, feel free to |
|
9 // implement them, but do not make any changes to the |
|
10 // templates below. Also have a look whether the functions |
|
11 // at the end of the file are of any help. |
|
12 |
|
13 |
5 |
14 |
6 type Pos = (Int, Int) // a position on a chessboard |
15 type Pos = (Int, Int) // a position on a chessboard |
7 type Path = List[Pos] // a path...a list of positions |
16 type Path = List[Pos] // a path...a list of positions |
8 |
17 |
|
18 //(1) Complete the function that tests whether the position x |
|
19 // is inside the board and not yet element in the path. |
9 |
20 |
10 // for measuring time in the JAR |
21 def is_legal(dim: Int, path: Path, x: Pos) : Boolean = { |
|
22 (x._1 < dim) && (x._2 < dim) && (!path.contains(x)) |
|
23 } |
|
24 |
|
25 |
|
26 |
|
27 //(2) Complete the function that calculates for a position x |
|
28 // all legal onward moves that are not already in the path. |
|
29 // The moves should be ordered in a "clockwise" manner. |
|
30 |
|
31 def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = { |
|
32 val movesets = List( |
|
33 (x._1 + 1, x._2 + 2), |
|
34 (x._1 + 2, x._2 + 1), |
|
35 (x._1 + 2, x._2 - 1), |
|
36 (x._1 + 1, x._2 - 2), |
|
37 (x._1 - 1, x._2 - 2), |
|
38 (x._1 - 2, x._2 - 1), |
|
39 (x._1 - 2, x._2 + 1), |
|
40 (x._1 - 1, x._2 + 2) |
|
41 ) |
|
42 movesets.filter(is_legal(dim, path, _)) |
|
43 } |
|
44 |
|
45 |
|
46 //some testcases |
|
47 // |
|
48 //assert(legal_moves(8, Nil, (2,2)) == |
|
49 // List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4))) |
|
50 //assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6))) |
|
51 //assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == |
|
52 // List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4))) |
|
53 //assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6))) |
|
54 |
|
55 |
|
56 //(3) Complete the two recursive functions below. |
|
57 // They exhaustively search for knight's tours starting from the |
|
58 // given path. The first function counts all possible tours, |
|
59 // and the second collects all tours in a list of paths. |
|
60 |
|
61 def count_tours(dim: Int, path: Path) : Int = { |
|
62 if (dim <= 4) 0 else { |
|
63 if (path.length >= (dim * dim)) 1 else { |
|
64 val movesets = legal_moves(dim, path, path.head) |
|
65 (for (move <- movesets) yield count_tours(dim, move :: path)).sum |
|
66 } |
|
67 } |
|
68 } |
|
69 |
|
70 def enum_tours(dim: Int, path: Path) : List[Path] = { |
|
71 if (dim <= 4) Nil else { |
|
72 if (path.length >= (dim * dim)) List(path) else { |
|
73 val movesets = legal_moves(dim, path, path.head) |
|
74 (for (move <- movesets) yield enum_tours(dim, move :: path)).flatten |
|
75 } |
|
76 } |
|
77 } |
|
78 |
|
79 |
|
80 //(4) Implement a first-function that finds the first |
|
81 // element, say x, in the list xs where f is not None. |
|
82 // In that case Return f(x), otherwise None. If possible, |
|
83 // calculate f(x) only once. |
|
84 |
|
85 @tailrec |
|
86 def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = { |
|
87 xs match { |
|
88 case Nil => None |
|
89 case head :: rest => { |
|
90 val result = f(head) |
|
91 if (result.isEmpty) first(rest, f) else result |
|
92 } |
|
93 } |
|
94 } |
|
95 |
|
96 |
|
97 // testcases |
|
98 // |
|
99 //def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None |
|
100 // |
|
101 //first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo) // Some(List((4,0))) |
|
102 //first(List((1, 0),(2, 0),(3, 0)), foo) // None |
|
103 |
|
104 |
|
105 //(5) Implement a function that uses the first-function from (4) for |
|
106 // trying out onward moves, and searches recursively for a |
|
107 // knight tour on a dim * dim-board. |
|
108 |
|
109 def first_tour(dim: Int, path: Path) : Option[Path] = ??? |
|
110 |
|
111 |
|
112 |
|
113 /* Helper functions |
|
114 |
|
115 |
|
116 // for measuring time |
11 def time_needed[T](code: => T) : T = { |
117 def time_needed[T](code: => T) : T = { |
12 val start = System.nanoTime() |
118 val start = System.nanoTime() |
13 val result = code |
119 val result = code |
14 val end = System.nanoTime() |
120 val end = System.nanoTime() |
15 println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.") |
121 println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.") |
16 result |
122 result |
17 } |
123 } |
|
124 |
|
125 // can be called for example with |
|
126 // |
|
127 // time_needed(count_tours(dim, List((0, 0)))) |
|
128 // |
|
129 // in order to print out the time that is needed for |
|
130 // running count_tours |
|
131 |
18 |
132 |
19 // for printing a board |
133 // for printing a board |
20 def print_board(dim: Int, path: Path): Unit = { |
134 def print_board(dim: Int, path: Path): Unit = { |
21 println() |
135 println() |
22 for (i <- 0 until dim) { |
136 for (i <- 0 until dim) { |
25 } |
139 } |
26 println() |
140 println() |
27 } |
141 } |
28 } |
142 } |
29 |
143 |
30 def is_legal(dim: Int, path: Path, x: Pos): Boolean = |
|
31 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) |
|
32 |
144 |
33 // testcases |
|
34 //assert(is_legal(8, Nil, (3, 4)) == true) |
|
35 //assert(is_legal(8, List((4, 1), (1, 0)), (4, 1)) == false) |
|
36 //assert(is_legal(2, Nil, (0, 0)) == true) |
|
37 |
|
38 |
|
39 def add_pair(x: Pos, y: Pos): Pos = |
|
40 (x._1 + y._1, x._2 + y._2) |
|
41 |
|
42 def moves(x: Pos): List[Pos] = |
|
43 List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), |
|
44 (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x, _)) |
|
45 |
|
46 // 1 mark |
|
47 |
|
48 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = |
|
49 moves(x).filter(is_legal(dim, path, _)) |
|
50 |
|
51 |
|
52 |
|
53 // testcases |
|
54 //assert(legal_moves(8, Nil, (2,2)) == |
|
55 // List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4))) |
|
56 //assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6))) |
|
57 //assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == |
|
58 // List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4))) |
|
59 //assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6))) |
|
60 //assert(legal_moves(8, Nil, (0,1)) == List((1,3), (2,2), (2,0))) |
|
61 //assert(legal_moves(1, Nil, (0,0)) == List()) |
|
62 //assert(legal_moves(2, Nil, (0,0)) == List()) |
|
63 //assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1))) |
|
64 |
|
65 // 2 marks |
|
66 |
|
67 def tcount_tours(dim: Int, path: Path): Int = { |
|
68 if (path.length == dim * dim) 1 |
|
69 else |
|
70 (for (x <- legal_moves(dim, path, path.head)) yield tcount_tours(dim, x::path)).sum |
|
71 } |
|
72 |
|
73 def count_tours(dim: Int, path: Path) = |
|
74 time_needed(tcount_tours(dim: Int, path: Path)) |
|
75 |
|
76 |
|
77 def tenum_tours(dim: Int, path: Path): List[Path] = { |
|
78 if (path.length == dim * dim) List(path) |
|
79 else |
|
80 (for (x <- legal_moves(dim, path, path.head)) yield tenum_tours(dim, x::path)).flatten |
|
81 } |
|
82 |
|
83 def enum_tours(dim: Int, path: Path) = |
|
84 time_needed(tenum_tours(dim: Int, path: Path)) |
|
85 |
|
86 // test cases |
|
87 |
|
88 /* |
|
89 def count_all_tours(dim: Int) = { |
|
90 for (i <- (0 until dim).toList; |
|
91 j <- (0 until dim).toList) yield count_tours(dim, List((i, j))) |
|
92 } |
|
93 |
|
94 def enum_all_tours(dim: Int): List[Path] = { |
|
95 (for (i <- (0 until dim).toList; |
|
96 j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten |
|
97 } |
|
98 |
|
99 |
|
100 println("Number of tours starting from (0, 0)") |
|
101 |
|
102 for (dim <- 1 to 5) { |
|
103 println(s"${dim} x ${dim} " + time_needed(0, count_tours(dim, List((0, 0))))) |
|
104 } |
|
105 |
|
106 println("Number of tours starting from all fields") |
|
107 |
|
108 for (dim <- 1 to 5) { |
|
109 println(s"${dim} x ${dim} " + time_needed(0, count_all_tours(dim))) |
|
110 } |
|
111 |
|
112 for (dim <- 1 to 5) { |
|
113 val ts = enum_tours(dim, List((0, 0))) |
|
114 println(s"${dim} x ${dim} ") |
|
115 if (ts != Nil) { |
|
116 print_board(dim, ts.head) |
|
117 println(ts.head) |
|
118 } |
|
119 } |
|
120 */ |
145 */ |
121 |
146 |
122 // 1 mark |
|
123 |
|
124 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { |
|
125 case Nil => None |
|
126 case x::xs => { |
|
127 val result = f(x) |
|
128 if (result.isDefined) result else first(xs, f) |
|
129 } |
|
130 } |
147 } |
131 |
|
132 // test cases |
|
133 //def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None |
|
134 // |
|
135 //first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo) |
|
136 //first(List((1, 0),(2, 0),(3, 0)), foo) |
|
137 |
|
138 |
|
139 // 1 mark |
|
140 |
|
141 def tfirst_tour(dim: Int, path: Path): Option[Path] = { |
|
142 if (path.length == dim * dim) Some(path) |
|
143 else |
|
144 first(legal_moves(dim, path, path.head), (x:Pos) => tfirst_tour(dim, x::path)) |
|
145 } |
|
146 |
|
147 def first_tour(dim: Int, path: Path) = |
|
148 time_needed(tfirst_tour(dim: Int, path: Path)) |
|
149 |
|
150 |
|
151 /* |
|
152 for (dim <- 1 to 8) { |
|
153 val t = first_tour(dim, List((0, 0))) |
|
154 println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) |
|
155 } |
|
156 */ |
|
157 |
|
158 // 15 secs for 8 x 8 |
|
159 //val ts1 = time_needed(0,first_tour(8, List((0, 0))).get) |
|
160 //val ts1 = time_needed(0,first_tour(8, List((1, 1))).get) |
|
161 |
|
162 // no result for 4 x 4 |
|
163 //val ts2 = time_needed(0, first_tour(4, List((0, 0)))) |
|
164 |
|
165 // 0.3 secs for 6 x 6 |
|
166 //val ts3 = time_needed(0, first_tour(6, List((0, 0)))) |
|
167 |
|
168 // 15 secs for 8 x 8 |
|
169 //time_needed(0, print_board(8, first_tour(8, List((0, 0))).get)) |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 } |
|
176 |
|
177 |
|