updated
authorChristian Urban <urbanc@in.tum.de>
Tue, 14 Nov 2017 22:19:04 +0000
changeset 146 61d9a5ac6430
parent 145 d306102fd33b
child 147 72f7dd1a3754
updated
templates2/knight1.scala
templates2/knight2.scala
templates2/knight3.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates2/knight1.scala	Tue Nov 14 22:19:04 2017 +0000
@@ -0,0 +1,42 @@
+// Part 1 about finding and counting Knight's tours
+//==================================================
+
+object CW7a {
+
+type Pos = (Int, Int)    // a position on a chessboard 
+type Path = List[Pos]    // a path...a list of positions
+
+//(1a) Complete the function that tests whether the position 
+//     is inside the board and not yet element in the path.
+
+//def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = ...
+
+
+//(1b) Complete the function that calculates for a position 
+//     all legal onward moves that are not already in the path. 
+//     The moves should be ordered in a "clockwise" manner.
+ 
+//def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ...
+
+
+//some test cases
+//
+//assert(legal_moves(8, Nil, (2,2)) == 
+//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
+//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
+//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
+//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
+//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
+
+
+//(1c) Complete the two recursive functions below. 
+//     They exhaustively search for knight's tours starting from the 
+//     given path. The first function counts all possible tours, 
+//     and the second collects all tours in a list of paths.
+
+//def count_tours(dim: Int, path: Path) : Int = ...
+
+//def enum_tours(dim: Int, path: Path) : List[Path] = ...
+
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates2/knight2.scala	Tue Nov 14 22:19:04 2017 +0000
@@ -0,0 +1,26 @@
+// Part 2 about finding a single tour for a board
+//================================================
+
+// copy any function you need from file knight1.scala
+
+object CW7b {
+
+type Pos = (Int, Int)    // a position on a chessboard 
+type Path = List[Pos]    // a path...a list of positions
+
+
+//(2a) Implement a first-function that finds the first 
+//     element, say x, in the list xs where f is not None. 
+//     In that case return f(x), otherwise None. If possible,
+//     calculate f(x) only once.
+
+//def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ...
+
+//(2b) Implement a function that uses the first-function for
+//     trying out onward moves, and searches recursively for a
+//     knight tour on a dim * dim-board.
+
+//def first_tour(dim: Int, path: Path) : Option[Path] = ...
+ 
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates2/knight3.scala	Tue Nov 14 22:19:04 2017 +0000
@@ -0,0 +1,33 @@
+// Part 3 about finding a single tour using the Warnsdorf Rule
+//=============================================================
+
+// copy any function you need from files knight1.scala and
+// knight2.scala
+
+object CW7c {
+
+type Pos = (Int, Int)    // a position on a chessboard 
+type Path = List[Pos]    // a path...a list of positions
+
+//(3a) Complete the function that calculates a list of onward
+//     moves like in (1b) but orders them according to Warnsdorf’s 
+//     rule. That means moves with the fewest legal onward moves 
+//     should come first.
+
+//def ordered_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ..
+
+
+//(3b) Complete the function that searches for a single *closed* 
+//     tour using the ordered moves function.
+
+//def first_closed_tour_heuristic(dim: Int, path: Path) : Option[Path] = ...
+
+
+//(3c) Same as (3b) but searches for *non-closed* tours. However, 
+//     you have to be careful to write a tail-recursive version as this 
+//     function will be called with dimensions of up to 40 * 40.
+
+//def first_tour_heuristic(dim: Int, path: Path) : Option[Path] = ...
+
+
+}