476
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
% !TEX program = xelatex
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
\documentclass{article}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
\usepackage{chessboard}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
\usepackage[LSBC4,T1]{fontenc}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
\let\clipbox\relax
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
\usepackage{../styles/style}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
\usepackage{../styles/langs}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
\usepackage{disclaimer}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
\usepackage{ulem}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
\begin{document}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
\setchessboard{smallboard,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
zero,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
showmover=false,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
boardfontencoding=LSBC4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
hlabelformat=\arabic{ranklabel},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
vlabelformat=\arabic{filelabel}}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
\mbox{}\\[-18mm]\mbox{}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
\section*{Main Part 4 (Scala, 11 Marks)}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
\mbox{}\hfill\textit{``The problem with object-oriented languages is they’ve got all this implicit,}\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
\mbox{}\hfill\textit{environment that they carry around with them. You wanted a banana but}\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
\mbox{}\hfill\textit{what you got was a gorilla holding the banana and the entire jungle.''}\smallskip\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
\mbox{}\hfill\textit{ --- Joe Armstrong (creator of the Erlang programming language)}\medskip\bigskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
This part is about searching and backtracking. You are asked to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
implement Scala programs that solve various versions of the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
\textit{Knight's Tour Problem} on a chessboard.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
\medskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
% Note the core, more advanced, part might include material you have not
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
%yet seen in the first three lectures. \bigskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
\IMPORTANTNONE{}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
Also note that the running time of each part will be restricted to a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
maximum of 30 seconds on my laptop: If you calculate a result once,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
try to avoid to calculate the result again. Feel free to copy any code
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
you need from files \texttt{knight1.scala}, \texttt{knight2.scala} and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
\texttt{knight3.scala}.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
\DISCLAIMER{}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
\subsection*{Background}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
The \textit{Knight's Tour Problem} is about finding a tour such that
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
the knight visits every field on an $n\times n$ chessboard once. For
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
example on a $5\times 5$ chessboard, a knight's tour is:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
\chessboard[maxfield=d4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
text = \small 24, markfield=Z4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
text = \small 11, markfield=a4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
text = \small 6, markfield=b4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
text = \small 17, markfield=c4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
text = \small 0, markfield=d4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
text = \small 19, markfield=Z3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
text = \small 16, markfield=a3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
text = \small 23, markfield=b3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
text = \small 12, markfield=c3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
text = \small 7, markfield=d3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
text = \small 10, markfield=Z2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
text = \small 5, markfield=a2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
text = \small 18, markfield=b2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
text = \small 1, markfield=c2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
text = \small 22, markfield=d2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
text = \small 15, markfield=Z1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
text = \small 20, markfield=a1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
text = \small 3, markfield=b1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
text = \small 8, markfield=c1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
text = \small 13, markfield=d1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
text = \small 4, markfield=Z0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
text = \small 9, markfield=a0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
text = \small 14, markfield=b0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
text = \small 21, markfield=c0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
text = \small 2, markfield=d0
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
This tour starts in the right-upper corner, then moves to field
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
$(3,2)$, then $(4,0)$ and so on. There are no knight's tours on
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
$2\times 2$, $3\times 3$ and $4\times 4$ chessboards, but for every
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
bigger board there is.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
A knight's tour is called \emph{closed}, if the last step in the tour
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
is within a knight's move to the beginning of the tour. So the above
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
knight's tour is \underline{not} closed because the last
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
step on field $(0, 4)$ is not within the reach of the first step on
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
$(4, 4)$. It turns out there is no closed knight's tour on a $5\times
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
5$ board. But there are on a $6\times 6$ board and on bigger ones, for
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
example
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
\chessboard[maxfield=e5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
pgfstyle={[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
text = \small 10, markfield=Z5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
text = \small 5, markfield=a5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
text = \small 18, markfield=b5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
text = \small 25, markfield=c5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
text = \small 16, markfield=d5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
text = \small 7, markfield=e5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
text = \small 31, markfield=Z4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
text = \small 26, markfield=a4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
text = \small 9, markfield=b4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
text = \small 6, markfield=c4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
text = \small 19, markfield=d4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
text = \small 24, markfield=e4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
% 4 11 30 17 8 15
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
text = \small 4, markfield=Z3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
text = \small 11, markfield=a3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
text = \small 30, markfield=b3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
text = \small 17, markfield=c3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
text = \small 8, markfield=d3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
text = \small 15, markfield=e3,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
%29 32 27 0 23 20
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
text = \small 29, markfield=Z2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
text = \small 32, markfield=a2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
text = \small 27, markfield=b2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
text = \small 0, markfield=c2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
text = \small 23, markfield=d2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
text = \small 20, markfield=e2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
%12 3 34 21 14 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
text = \small 12, markfield=Z1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
text = \small 3, markfield=a1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
text = \small 34, markfield=b1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
text = \small 21, markfield=c1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
text = \small 14, markfield=d1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
text = \small 1, markfield=e1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
%33 28 13 2 35 22
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
text = \small 33, markfield=Z0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
text = \small 28, markfield=a0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
text = \small 13, markfield=b0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
text = \small 2, markfield=c0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
text = \small 35, markfield=d0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
text = \small 22, markfield=e0,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
vlabel=false,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
hlabel=false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
where the 35th move can join up again with the 0th move.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
If you cannot remember how a knight moves in chess, or never played
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
chess, below are all potential moves indicated for two knights, one on
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
field $(2, 2)$ (blue moves) and another on $(7, 7)$ (red moves):
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
{\chessboard[maxfield=g7,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
color=blue!50,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
linewidth=0.2em,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
shortenstart=0.5ex,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
shortenend=0.5ex,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
markstyle=cross,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
markfields={a4, c4, Z3, d3, Z1, d1, a0, c0},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
color=red!50,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
markfields={f5, e6},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
setpieces={Ng7, Nb2},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
boardfontsize=12pt,labelfontsize=9pt]}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
\subsection*{Reference Implementation}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
%\mbox{}\alert{}\textcolor{red}{You need to download \texttt{knight1.jar} from K%EATS. The one
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
%supplied with github does not contain the correct code. See Scala coursework
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
%section on KEATS.}\medskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
This Scala part comes with three reference implementations in form of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
\texttt{jar}-files. This allows you to run any test cases on your own
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
computer. For example you can call Scala on the command line with the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
option \texttt{-cp knight1.jar} and then query any function from the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
\texttt{knight1.scala} template file. As usual you have to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
prefix the calls with \texttt{M4a}, \texttt{M4b}, \texttt{M4c} and \texttt{M4d}.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
Since some of the calls are time sensitive, I included some timing
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
information. For example
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
$ scala -cp knight1.jar
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
scala> M4a.enum_tours(5, List((0, 0))).length
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
Time needed: 1.722 secs.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
res0: Int = 304
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
scala> M4a.print_board(8, M4a.first_tour(8, List((0, 0))).get)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
Time needed: 15.411 secs.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
51 46 55 44 53 4 21 12
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
56 43 52 3 22 13 24 5
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
47 50 45 54 25 20 11 14
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
42 57 2 49 40 23 6 19
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
35 48 41 26 61 10 15 28
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
58 1 36 39 32 27 18 7
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
37 34 31 60 9 62 29 16
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
0 59 38 33 30 17 8 63
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
\end{lstlisting}%$
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
198 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
\subsection*{Hints}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
Useful list functions: \texttt{.contains(..)} checks
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
whether an element is in a list, \texttt{.flatten} turns a list of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
205 |
lists into just a list, \texttt{\_::\_} puts an element on the head of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
the list, \texttt{.head} gives you the first element of a list (make
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
sure the list is not \texttt{Nil}); a useful option function:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
\texttt{.isDefined} returns true, if an option is \texttt{Some(..)};
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
anonymous functions can be constructed using \texttt{(x:Int) => ...},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
this function takes an \texttt{Int} as an argument;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
211 |
a useful list function: \texttt{.sortBy} sorts a list
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
212 |
according to a component given by the function; a function can be
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
213 |
tested to be tail-recursive by annotation \texttt{@tailrec}, which is
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
214 |
made available by importing \texttt{scala.annotation.tailrec}.\medskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
215 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
216 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
217 |
%%\newpage
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
218 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
\subsection*{Tasks}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
You are asked to implement the knight's tour problem such that the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
dimension of the board can be changed. Therefore most functions will
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
take the dimension of the board as an argument. The fun with this
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
224 |
problem is that even for small chessboard dimensions it has already an
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
incredibly large search space---finding a tour is like finding a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
needle in a haystack. In the first task we want to see how far we get
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
with exhaustively exploring the complete search space for small
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
chessboards.\medskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
Let us first fix the basic datastructures for the implementation. The
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
board dimension is an integer.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
A \emph{position} (or field) on the chessboard is
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
a pair of integers, like $(0, 0)$. A \emph{path} is a list of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
positions. The first (or 0th move) in a path is the last element in
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
this list; and the last move in the path is the first element. For
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
237 |
example the path for the $5\times 5$ chessboard above is represented
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
238 |
by
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
239 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
240 |
\[
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
241 |
\texttt{List($\underbrace{\texttt{(0, 4)}}_{24}$,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
242 |
$\underbrace{\texttt{(2, 3)}}_{23}$, ...,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
243 |
$\underbrace{\texttt{(3, 2)}}_1$, $\underbrace{\texttt{(4, 4)}}_0$)}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
244 |
\]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
245 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
246 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
247 |
Suppose the dimension of a chessboard is $n$, then a path is a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
248 |
\emph{tour} if the length of the path is $n \times n$, each element
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
249 |
occurs only once in the path, and each move follows the rules of how a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
250 |
knight moves (see above for the rules).
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
251 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
252 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
253 |
\subsubsection*{Task 1 (file knight1.scala)}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
254 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
255 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
256 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
257 |
\begin{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
258 |
\item[(1)] Implement an \texttt{is\_legal} function that takes a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
259 |
dimension, a path and a position as arguments and tests whether the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
260 |
position is inside the board and not yet element in the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
261 |
path. \hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
262 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
263 |
\item[(2)] Implement a \texttt{legal\_moves} function that calculates for a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
264 |
position all legal onward moves. If the onward moves are
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
265 |
placed on a circle, you should produce them starting from
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
266 |
``12-o'clock'' following in clockwise order. For example on an
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
267 |
$8\times 8$ board for a knight at position $(2, 2)$ and otherwise
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
268 |
empty board, the legal-moves function should produce the onward
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
269 |
positions in this order:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
270 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
271 |
\begin{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
272 |
\texttt{List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4))}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
273 |
\end{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
274 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
275 |
If the board is not empty, then maybe some of the moves need to be
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
276 |
filtered out from this list. For a knight on field $(7, 7)$ and an
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
277 |
empty board, the legal moves are
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
278 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
279 |
\begin{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
280 |
\texttt{List((6,5), (5,6))}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
281 |
\end{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
282 |
\mbox{}\hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
283 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
284 |
\item[(3)] Implement two recursive functions (\texttt{count\_tours} and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
285 |
\texttt{enum\_tours}). They each take a dimension and a path as
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
286 |
arguments. They exhaustively search for tours starting
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
287 |
from the given path. The first function counts all possible
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
288 |
tours (there can be none for certain board sizes) and the second
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
289 |
collects all tours in a list of paths. These functions will be
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
290 |
called with a path containing a single position---the starting field.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
291 |
They are expected to extend this path so as to find all tours starting
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
292 |
from the given position.\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
293 |
\mbox{}\hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
294 |
\end{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
295 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
296 |
\noindent \textbf{Test data:} For the marking, the functions in (3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
297 |
will be called with board sizes up to $5 \times 5$. If you search
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
298 |
for tours on a $5 \times 5$ board starting only from field $(0, 0)$,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
299 |
there are 304 of tours. If you try out every field of a $5 \times
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
300 |
5$-board as a starting field and add up all tours, you obtain
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
301 |
1728. A $6\times 6$ board is already too large to be searched
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
302 |
exhaustively.\footnote{For your interest, the number of tours on
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
303 |
$6\times 6$, $7\times 7$ and $8\times 8$ are 6637920, 165575218320,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
304 |
19591828170979904, respectively.}\smallskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
305 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
306 |
\begin{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
307 |
\item[(4)] Implement a \texttt{first}-function. This function takes a list of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
308 |
positions and a function $f$ as arguments; $f$ is the name we give to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
309 |
this argument). The function $f$ takes a position as argument and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
310 |
produces an optional path. So $f$'s type is \texttt{Pos =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
311 |
Option[Path]}. The idea behind the \texttt{first}-function is as follows:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
312 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
313 |
\[
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
314 |
\begin{array}{lcl}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
315 |
\textit{first}(\texttt{Nil}, f) & \dn & \texttt{None}\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
316 |
\textit{first}(x\!::\!xs, f) & \dn & \begin{cases}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
317 |
f(x) & \textit{if}\;f(x) \not=\texttt{None}\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
318 |
\textit{first}(xs, f) & \textit{otherwise}\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
319 |
\end{cases}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
320 |
\end{array}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
321 |
\]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
322 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
323 |
\noindent That is, we want to find the first position where the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
324 |
result of $f$ is not \texttt{None}, if there is one. Note that
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
325 |
`inside' \texttt{first}, you do not (need to) know anything about
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
326 |
the argument $f$ except its type, namely \texttt{Pos =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
327 |
Option[Path]}. If you want to find out what the result of $f$ is
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
328 |
on a particular argument, say $x$, you can just write $f(x)$.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
329 |
There is one additional point however you should
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
330 |
take into account when implementing \texttt{first}: you will need to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
331 |
calculate what the result of $f(x)$ is; your code should do this
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
332 |
only \textbf{once} and for as \textbf{few} elements in the list as
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
333 |
possible! Do not calculate $f(x)$ for all elements and then see which
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
334 |
is the first \texttt{Some}.\\\mbox{}\hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
335 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
336 |
\item[(5)] Implement a \texttt{first\_tour} function that uses the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
337 |
\texttt{first}-function from (4), and searches recursively for single tour.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
338 |
As there might not be such a tour at all, the \texttt{first\_tour} function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
339 |
needs to return a value of type
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
340 |
\texttt{Option[Path]}.\\\mbox{}\hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
341 |
\end{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
342 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
343 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
344 |
\textbf{Testing:} The \texttt{first\_tour} function will be called with board
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
345 |
sizes of up to $8 \times 8$.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
346 |
\bigskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
347 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
348 |
%%\newpage
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
349 |
\subsubsection*{Task 2 (file knight2.scala)}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
350 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
351 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
352 |
As you should have seen in the earlier parts, a naive search for tours beyond
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
353 |
$8 \times 8$ boards and also searching for closed tours even on small
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
354 |
boards takes too much time. There is a heuristics, called \emph{Warnsdorf's
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
355 |
Rule} that can speed up finding a tour. This heuristics states that a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
356 |
knight is moved so that it always proceeds to the field from which the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
357 |
knight will have the \underline{fewest} onward moves. For example for
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
358 |
a knight on field $(1, 3)$, the field $(0, 1)$ has the fewest possible
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
359 |
onward moves, namely 2.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
360 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
361 |
\chessboard[maxfield=g7,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
362 |
pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
363 |
text = \small 3, markfield=Z5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
364 |
text = \small 7, markfield=b5,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
365 |
text = \small 7, markfield=c4,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
366 |
text = \small 7, markfield=c2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
367 |
text = \small 5, markfield=b1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
368 |
text = \small 2, markfield=Z1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
369 |
setpieces={Na3}]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
370 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
371 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
372 |
Warnsdorf's Rule states that the moves on the board above should be
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
373 |
tried in the order
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
374 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
375 |
\[
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
376 |
(0, 1), (0, 5), (2, 1), (2, 5), (3, 4), (3, 2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
377 |
\]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
378 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
379 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
380 |
Whenever there are ties, the corresponding onward moves can be in any
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
381 |
order. When calculating the number of onward moves for each field, we
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
382 |
do not count moves that revisit any field already visited.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
383 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
384 |
\begin{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
385 |
\item[(6)] Write a function \texttt{ordered\_moves} that calculates a list of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
386 |
onward moves like in (2) but orders them according to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
387 |
Warnsdorf’s Rule. That means moves with the fewest legal onward moves
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
388 |
should come first (in order to be tried out first). \hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
389 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
390 |
\item[(7)] Implement a \texttt{first\_closed\_tour\_heuristics}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
391 |
function that searches for a single
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
392 |
\textbf{closed} tour on a $6\times 6$ board. It should try out
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
393 |
onward moves according to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
394 |
the \texttt{ordered\_moves} function from (6). It is more likely to find
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
395 |
a solution when started in the middle of the board (that is
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
396 |
position $(dimension / 2, dimension / 2)$). \hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
397 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
398 |
\item[(8)] Implement a \texttt{first\_tour\_heuristics} function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
399 |
for boards up to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
400 |
$30\times 30$. It is the same function as in (7) but searches for
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
401 |
tours (not just closed tours). It might be called with any field on the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
402 |
board as starting field.\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
403 |
%You have to be careful to write a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
404 |
%tail-recursive function of the \texttt{first\_tour\_heuristics} function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
405 |
%otherwise you will get problems with stack-overflows.\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
406 |
\mbox{}\hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
407 |
\end{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
408 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
409 |
\subsubsection*{Task 3 (file knight3.scala)}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
410 |
\begin{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
411 |
\item[(9)] Implement a function \texttt{tour\_on\_mega\_board} which is
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
412 |
the same function as in (8), \textbf{but} should be able to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
413 |
deal with boards up to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
414 |
$70\times 70$ \textbf{within 30 seconds} (on my laptop). This will be tested
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
415 |
by starting from field $(0, 0)$. You have to be careful to
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
416 |
write a tail-recursive function otherwise you will get problems
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
417 |
with stack-overflows. Please observe the requirements about
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
418 |
the submissions: no tricks involving \textbf{.par}.\medskip
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
419 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
420 |
The timelimit of 30 seconds is with respect to the laptop on which the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
421 |
marking will happen. You can roughly estimate how well your
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
422 |
implementation performs by running \texttt{knight3.jar} on your
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
423 |
computer. For example the reference implementation shows
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
424 |
on my laptop:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
425 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
426 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
427 |
$ scala -cp knight3.jar
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
428 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
429 |
scala> M4c.tour_on_mega_board(70, List((0, 0)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
430 |
Time needed: 9.484 secs.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
431 |
...<<long_list>>...
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
432 |
\end{lstlisting}%$
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
433 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
434 |
\mbox{}\hfill[1 Mark]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
435 |
\end{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
436 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
437 |
\subsubsection*{Task 4 (file knight4.scala)}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
438 |
\begin{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
439 |
\item[(10)] In this task we want to solve the problem of finding a single
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
440 |
tour (if there exists one) on what is sometimes called ``mutilated''
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
441 |
chessboards, for example rectangular chessbords or chessboards where
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
442 |
fields are missing. For this implement a search function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
443 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
444 |
\begin{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
445 |
\begin{tabular}{@{}l@{}}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
446 |
\texttt{def one\_tour\_pred(dim: Int, path: Path,}\\
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
447 |
\texttt{\phantom{def one\_tour\_pred(}n: Int, f: Pos => Boolean): Option[Path]}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
448 |
\end{tabular}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
449 |
\end{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
450 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
451 |
which has, like before, the dimension and current path as arguments,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
452 |
and in addtion it takes an integer, which specifies the length of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
453 |
the longest path (or length of the path after which the search
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
454 |
should backtrack), and a function from positions to Booleans. This
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
455 |
function acts as a predicate in order to restrict which onward legal
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
456 |
moves should be considered in the search. The function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
457 |
\texttt{one\_tour\_pred} should return a single tour
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
458 |
(\texttt{Some}-case), if one or more tours exist, and \texttt{None}, if no
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
459 |
tour exists. For example when called with
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
460 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
461 |
\begin{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
462 |
\texttt{one\_tour\_pred(7, List((0, 0)), 35, x => x.\_1 < 5)}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
463 |
\end{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
464 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
465 |
we are looking for a tour starting from position \texttt{(0,0)} on a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
466 |
7 $\times$ 5 board, where the maximum length of the path is 35. The
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
467 |
predicate \texttt{x => x.\_1 < 5} ensures that no position with an
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
468 |
x-coordinate bigger than 4 is considered. One possible solution is
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
469 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
470 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
471 |
0 13 22 33 28 11 20
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
472 |
23 32 1 12 21 34 27
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
473 |
14 7 16 29 2 19 10
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
474 |
31 24 5 8 17 26 3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
475 |
6 15 30 25 4 9 18
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
476 |
-1 -1 -1 -1 -1 -1 -1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
477 |
-1 -1 -1 -1 -1 -1 -1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
478 |
\end{lstlisting}%$
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
479 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
480 |
where \texttt{print\_board} prints a \texttt{-1} for all fields that
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
481 |
have not been visited.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
482 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
483 |
\mbox{}\hfill[2 Marks]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
484 |
\end{itemize}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
485 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
486 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
487 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
488 |
\end{document}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
489 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
490 |
%%% Local Variables:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
491 |
%%% mode: latex
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
492 |
%%% TeX-master: t
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
493 |
%%% End:
|