diff -r 126d0e47ac85 -r 274c865b3878 cws/main_cw04.tex --- a/cws/main_cw04.tex Wed Nov 02 13:22:59 2022 +0000 +++ b/cws/main_cw04.tex Wed Nov 02 21:49:42 2022 +0000 @@ -19,7 +19,7 @@ \mbox{}\\[-18mm]\mbox{} -\section*{Main Part 4 (Scala, 9 Marks)} +\section*{Main Part 4 (Scala, 11 Marks)} \mbox{}\hfill\textit{``The problem with object-oriented languages is they’ve got all this implicit,}\\ \mbox{}\hfill\textit{environment that they carry around with them. You wanted a banana but}\\ @@ -214,7 +214,7 @@ made available by importing \texttt{scala.annotation.tailrec}.\medskip -\newpage +%%\newpage \subsection*{Tasks} @@ -432,9 +432,56 @@ \end{lstlisting}%$ \mbox{}\hfill[1 Mark] -\end{itemize} -\bigskip +\end{itemize} + +\subsubsection*{Task 4 (file knight4.scala)} +\begin{itemize} +\item[(10)] In this task we want to solve the problem of finding a single + tour (if there exists one) on what is sometimes called ``mutilated'' + chessboards, for example rectangular chessbords or chessboards where + fields are missing. For this implement a search function + + \begin{center} + \begin{tabular}{@{}l@{}} + \texttt{def one\_tour\_pred(dim: Int, path: Path,}\\ + \texttt{\phantom{def one\_tour\_pred(}n: Int, f: Pos => Boolean): Option[Path]} + \end{tabular} + \end{center} + which has, like before, the dimension and current path as arguments, + and in addtion it takes an integer, which specifies the length of + the longest path (or length of the path after which the search + should backtrack), and a function from positions to Booleans. This + function acts as a predicate in order to restrict which onward legal + moves should be considered in the search. The function + \texttt{one\_tour\_pred} should return a single tour + (\texttt{Some}-case), if one or more tours exist, and \texttt{None}, if no + tour exists. For example when called with + + \begin{center} + \texttt{one\_tour\_pred(7, List((0, 0)), 35, x => x.\_1 < 5)} + \end{center} + + we are looking for a tour starting from position \texttt{(0,0)} on a + 7 $\times$ 5 board, where the maximum length of the path is 35. The + predicate \texttt{x => x.\_1 < 5} ensures that no position with an + x-coordinate bigger than 4 is considered. One possible solution is + + \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] + 0 13 22 33 28 11 20 + 23 32 1 12 21 34 27 + 14 7 16 29 2 19 10 + 31 24 5 8 17 26 3 + 6 15 30 25 4 9 18 + -1 -1 -1 -1 -1 -1 -1 + -1 -1 -1 -1 -1 -1 -1 +\end{lstlisting}%$ + +where \texttt{print\_board} prints a \texttt{-1} for all fields that +have not been visited. + + \mbox{}\hfill[2 Marks] +\end{itemize}