cws/cw02.tex
author Christian Urban <urbanc@in.tum.de>
Tue, 26 Jun 2018 01:49:32 +0100
changeset 187 4d300409f2fe
parent 166 780c40aaad27
child 202 f7bcb27d1940
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\usepackage{chessboard}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
\usepackage[LSBC4,T1]{fontenc}
149
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
     4
\let\clipbox\relax
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
     5
\usepackage{../style}
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
     6
\usepackage{disclaimer}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
\setchessboard{smallboard,
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    11
               zero,
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
               showmover=false,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
               boardfontencoding=LSBC4,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
               hlabelformat=\arabic{ranklabel},
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
               vlabelformat=\arabic{filelabel}}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    17
\mbox{}\\[-18mm]\mbox{}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    19
\section*{Coursework 7 (Scala, Knight's Tour)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
    21
This coursework is worth 10\%. It is about searching and
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
    22
backtracking. The first part is due on 23 November at 11pm; the
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    23
second, more advanced part, is due on 21 December at 11pm. You are
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
    24
asked to implement Scala programs that solve various versions of the
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
    25
\textit{Knight's Tour Problem} on a chessboard. Note the second part
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
    26
might include material you have not yet seen in the first two
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    27
lectures. \bigskip
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
    28
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
    29
\IMPORTANT{}
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    30
Also note that the running time of each part will be restricted to a
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    31
maximum of 360 seconds on my laptop: If you calculate a result once,
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    32
try to avoid to calculate the result again. Feel free to copy any code
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    33
you need from files \texttt{knight1.scala}, \texttt{knight2.scala} and
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    34
\texttt{knight3.scala}.
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    35
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
    36
\DISCLAIMER{}
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    37
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    38
\subsection*{Background}
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    40
The \textit{Knight's Tour Problem} is about finding a tour such that
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
    41
the knight visits every field on an $n\times n$ chessboard once. For
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
    42
example on a $5\times 5$ chessboard, a knight's tour is:
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    43
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    44
\chessboard[maxfield=d4, 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    45
            pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    46
            text = \small 24, markfield=Z4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    47
            text = \small 11, markfield=a4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    48
            text = \small  6, markfield=b4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    49
            text = \small 17, markfield=c4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    50
            text = \small  0, markfield=d4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    51
            text = \small 19, markfield=Z3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    52
            text = \small 16, markfield=a3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    53
            text = \small 23, markfield=b3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    54
            text = \small 12, markfield=c3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    55
            text = \small  7, markfield=d3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    56
            text = \small 10, markfield=Z2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    57
            text = \small  5, markfield=a2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    58
            text = \small 18, markfield=b2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    59
            text = \small  1, markfield=c2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    60
            text = \small 22, markfield=d2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    61
            text = \small 15, markfield=Z1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    62
            text = \small 20, markfield=a1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    63
            text = \small  3, markfield=b1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    64
            text = \small  8, markfield=c1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    65
            text = \small 13, markfield=d1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    66
            text = \small  4, markfield=Z0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    67
            text = \small  9, markfield=a0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    68
            text = \small 14, markfield=b0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    69
            text = \small 21, markfield=c0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    70
            text = \small  2, markfield=d0
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    71
           ]
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    72
           
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    73
\noindent
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    74
The tour starts in the right-upper corner, then moves to field
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    75
$(3,2)$, then $(4,0)$ and so on. There are no knight's tours on
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    76
$2\times 2$, $3\times 3$ and $4\times 4$ chessboards, but for every
74
fb2d8012ed08 updated
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    77
bigger board there is. 
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    78
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    79
A knight's tour is called \emph{closed}, if the last step in the tour
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    80
is within a knight's move to the beginning of the tour. So the above
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
    81
knight's tour is \underline{not} closed because the last
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    82
step on field $(0, 4)$ is not within the reach of the first step on
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    83
$(4, 4)$. It turns out there is no closed knight's tour on a $5\times
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
    84
5$ board. But there are on a $6\times 6$ board and on bigger ones, for
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
    85
example
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
\chessboard[maxfield=e5, 
147
72f7dd1a3754 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    88
            pgfstyle={[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    89
            text = \small 10, markfield=Z5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    90
            text = \small  5, markfield=a5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    91
            text = \small 18, markfield=b5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    92
            text = \small 25, markfield=c5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    93
            text = \small 16, markfield=d5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    94
            text = \small  7, markfield=e5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    95
            text = \small 31, markfield=Z4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    96
            text = \small 26, markfield=a4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    97
            text = \small  9, markfield=b4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    98
            text = \small  6, markfield=c4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    99
            text = \small 19, markfield=d4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   100
            text = \small 24, markfield=e4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   101
            % 4  11  30  17   8  15 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   102
            text = \small  4, markfield=Z3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   103
            text = \small 11, markfield=a3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   104
            text = \small 30, markfield=b3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   105
            text = \small 17, markfield=c3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   106
            text = \small  8, markfield=d3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   107
            text = \small 15, markfield=e3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   108
            %29  32  27   0  23  20 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   109
            text = \small 29, markfield=Z2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   110
            text = \small 32, markfield=a2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   111
            text = \small 27, markfield=b2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   112
            text = \small  0, markfield=c2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   113
            text = \small 23, markfield=d2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   114
            text = \small 20, markfield=e2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   115
            %12   3  34  21  14   1 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   116
            text = \small 12, markfield=Z1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   117
            text = \small  3, markfield=a1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   118
            text = \small 34, markfield=b1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   119
            text = \small 21, markfield=c1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   120
            text = \small 14, markfield=d1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   121
            text = \small  1, markfield=e1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   122
            %33  28  13   2  35  22 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   123
            text = \small 33, markfield=Z0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   124
            text = \small 28, markfield=a0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   125
            text = \small 13, markfield=b0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   126
            text = \small  2, markfield=c0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   127
            text = \small 35, markfield=d0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   128
            text = \small 22, markfield=e0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   129
            vlabel=false,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   130
            hlabel=false
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
           ]
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   133
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
\noindent
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   135
where the 35th move can join up again with the 0th move.
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   136
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   137
If you cannot remember how a knight moves in chess, or never played
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   138
chess, below are all potential moves indicated for two knights, one on
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   139
field $(2, 2)$ (blue moves) and another on $(7, 7)$ (red moves):
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   140
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   141
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   142
\chessboard[maxfield=g7,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   143
            color=blue!50,
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
            linewidth=0.2em,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
            shortenstart=0.5ex,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
            shortenend=0.5ex,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
            markstyle=cross,
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   148
            markfields={a4, c4, Z3, d3, Z1, d1, a0, c0},
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
            color=red!50,
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   150
            markfields={f5, e6},
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   151
            setpieces={Ng7, Nb2}]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   152
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   153
\subsection*{Part 1 (7 Marks)}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   154
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   155
You are asked to implement the knight's tour problem such that the
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   156
dimension of the board can be changed.  Therefore most functions will
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   157
take the dimension of the board as an argument.  The fun with this
60
f099bcf9cff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 59
diff changeset
   158
problem is that even for small chessboard dimensions it has already an
f099bcf9cff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 59
diff changeset
   159
incredibly large search space---finding a tour is like finding a
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   160
needle in a haystack. In the first task we want to see how far we get
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   161
with exhaustively exploring the complete search space for small
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   162
chessboards.\medskip
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   164
\noindent
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   165
Let us first fix the basic datastructures for the implementation.  The
60
f099bcf9cff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 59
diff changeset
   166
board dimension is an integer (we will never go beyond board sizes of
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   167
$40 \times 40$).  A \emph{position} (or field) on the chessboard is
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   168
a pair of integers, like $(0, 0)$. A \emph{path} is a list of
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   169
positions. The first (or 0th move) in a path is the last element in
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   170
this list; and the last move in the path is the first element. For
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   171
example the path for the $5\times 5$ chessboard above is represented
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   172
by
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   174
\[
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   175
\texttt{List($\underbrace{\texttt{(0, 4)}}_{24}$,
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   176
  $\underbrace{\texttt{(2, 3)}}_{23}$, ...,
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   177
  $\underbrace{\texttt{(3, 2)}}_1$, $\underbrace{\texttt{(4, 4)}}_0$)}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   178
\]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   179
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   180
\noindent
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   181
Suppose the dimension of a chessboard is $n$, then a path is a
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   182
\emph{tour} if the length of the path is $n \times n$, each element
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   183
occurs only once in the path, and each move follows the rules of how a
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   184
knight moves (see above for the rules).
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   187
\subsubsection*{Tasks (file knight1.scala)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   188
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   189
\begin{itemize}
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   190
\item[(1a)] Implement an \texttt{is\_legal\_move} function that takes a
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   191
  dimension, a path and a position as arguments and tests whether the
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   192
  position is inside the board and not yet element in the
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   193
  path. \hfill[1 Mark]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   194
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   195
\item[(1b)] Implement a \texttt{legal\_moves} function that calculates for a
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   196
  position all legal onward moves. If the onward moves are
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   197
  placed on a circle, you should produce them starting from
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   198
  ``12-o'clock'' following in clockwise order.  For example on an
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   199
  $8\times 8$ board for a knight at position $(2, 2)$ and otherwise
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   200
  empty board, the legal-moves function should produce the onward
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   201
  positions in this order:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   203
  \begin{center}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   204
  \texttt{List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4))}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   205
  \end{center}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   206
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   207
  If the board is not empty, then maybe some of the moves need to be
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   208
  filtered out from this list.  For a knight on field $(7, 7)$ and an
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   209
  empty board, the legal moves are
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   210
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   211
  \begin{center}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   212
  \texttt{List((6,5), (5,6))}
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   213
  \end{center}
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   214
  \mbox{}\hfill[1 Mark]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   215
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   216
\item[(1c)] Implement two recursive functions (\texttt{count\_tours} and
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   217
  \texttt{enum\_tours}). They each take a dimension and a path as
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
   218
  arguments. They exhaustively search for tours starting
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
   219
  from the given path. The first function counts all possible 
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   220
  tours (there can be none for certain board sizes) and the second
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
   221
  collects all tours in a list of paths.\hfill[2 Marks]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   222
\end{itemize}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   224
\noindent \textbf{Test data:} For the marking, the functions in (1c)
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   225
will be called with board sizes up to $5 \times 5$. If you search
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
   226
for tours on a $5 \times 5$ board starting only from field $(0, 0)$,
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   227
there are 304 of tours. If you try out every field of a $5 \times
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
   228
5$-board as a starting field and add up all tours, you obtain
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   229
1728. A $6\times 6$ board is already too large to be searched
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 86
diff changeset
   230
exhaustively.\footnote{For your interest, the number of tours on
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   231
  $6\times 6$, $7\times 7$ and $8\times 8$ are 6637920, 165575218320,
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   232
  19591828170979904, respectively.}\bigskip
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   233
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   234
\noindent
149
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   235
\textbf{Hints:} useful list functions: \texttt{.contains(..)} checks
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   236
whether an element is in a list, \texttt{.flatten} turns a list of
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   237
lists into just a list, \texttt{\_::\_} puts an element on the head of
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   238
the list, \texttt{.head} gives you the first element of a list (make
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   239
sure the list is not \texttt{Nil}).
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   240
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   241
\subsubsection*{Tasks (file knight2.scala)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   242
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   243
\begin{itemize}
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   244
\item[(2a)] Implement a \texttt{first}-function. This function takes a list of
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   245
  positions and a function $f$ as arguments; $f$ is the name we give to
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   246
  this argument). The function $f$ takes a position as argument and
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   247
  produces an optional path. So $f$'s type is \texttt{Pos =>
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   248
    Option[Path]}. The idea behind the \texttt{first}-function is as follows:
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   249
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   250
  \[
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   251
  \begin{array}{lcl}
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   252
  \textit{first}(\texttt{Nil}, f) & \dn & \texttt{None}\\  
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   253
  \textit{first}(x\!::\!xs, f) & \dn & \begin{cases}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   254
    f(x) & \textit{if}\;f(x) \not=\texttt{None}\\
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   255
    \textit{first}(xs, f) & \textit{otherwise}\\
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   256
                              \end{cases}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   257
  \end{array}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   258
  \]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   259
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   260
  \noindent That is, we want to find the first position where the
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   261
  result of $f$ is not \texttt{None}, if there is one. Note that
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   262
  `inside' \texttt{first}, you do not (need to) know anything about
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   263
  the argument $f$ except its type, namely \texttt{Pos =>
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   264
    Option[Path]}. There is one additional point however you should
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   265
  take into account when implementing \texttt{first}: you will need to
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   266
  calculate what the result of $f(x)$ is; your code should do this
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   267
  only \textbf{once} and for as \textbf{few} elements in the list as
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   268
  possible! Do not calculate $f(x)$ for all elements and then see which 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   269
  is the first \texttt{Some}.\\\mbox{}\hfill[1 Mark]
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   270
  
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   271
\item[(2b)] Implement a \texttt{first\_tour} function that uses the
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   272
  \texttt{first}-function from (2a), and searches recursively for a tour.
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   273
  As there might not be such a tour at all, the \texttt{first\_tour} function
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   274
  needs to return a value of type
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   275
  \texttt{Option[Path]}.\\\mbox{}\hfill[2 Marks]
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   276
\end{itemize}
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   277
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   278
\noindent
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   279
\textbf{Testing:} The \texttt{first\_tour} function will be called with board
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   280
sizes of up to $8 \times 8$.
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   281
\bigskip
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   283
\noindent
149
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   284
\textbf{Hints:} a useful list function: \texttt{.filter(..)} filters a
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   285
list according to a boolean function; a useful option function:
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   286
\texttt{.isDefined} returns true, if an option is \texttt{Some(..)};
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   287
anonymous functions can be constructed using \texttt{(x:Int) => ...},
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   288
this functions takes an \texttt{Int} as an argument.
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   289
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   290
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   291
%%\newpage
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   292
\subsection*{Part 2 (3 Marks)}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   293
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   294
As you should have seen in Part 1, a naive search for tours beyond
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   295
$8 \times 8$ boards and also searching for closed tours even on small
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   296
boards takes too much time. There is a heuristic, called \emph{Warnsdorf's
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   297
Rule} that can speed up finding a tour. This heuristic states that a
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   298
knight is moved so that it always proceeds to the field from which the
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   299
knight will have the \underline{fewest} onward moves.  For example for
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   300
a knight on field $(1, 3)$, the field $(0, 1)$ has the fewest possible
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   301
onward moves, namely 2.
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   302
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   303
\chessboard[maxfield=g7,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   304
            pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   305
            text = \small 3, markfield=Z5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   306
            text = \small 7, markfield=b5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   307
            text = \small 7, markfield=c4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   308
            text = \small 7, markfield=c2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   309
            text = \small 5, markfield=b1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   310
            text = \small 2, markfield=Z1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   311
            setpieces={Na3}]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   312
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   313
\noindent
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   314
Warnsdorf's Rule states that the moves on the board above should be
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   315
tried in the order
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   316
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   317
\[
46
48d2cbe8ee5e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   318
(0, 1), (0, 5), (2, 1), (2, 5), (3, 4), (3, 2)
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   319
\]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   320
46
48d2cbe8ee5e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   321
\noindent
60
f099bcf9cff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 59
diff changeset
   322
Whenever there are ties, the corresponding onward moves can be in any
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   323
order.  When calculating the number of onward moves for each field, we
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   324
do not count moves that revisit any field already visited.
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   325
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   326
\subsubsection*{Tasks (file knight3.scala)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   327
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   328
\begin{itemize}
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   329
\item[(3a)] Write a function \texttt{ordered\_moves} that calculates a list of
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   330
  onward moves like in (1b) but orders them according to the
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   331
  Warnsdorf’s Rule. That means moves with the fewest legal onward moves
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
   332
  should come first (in order to be tried out first). \hfill[1 Mark]
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   333
  
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   334
\item[(3b)] Implement a \texttt{first\_closed-tour\_heuristic}
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   335
  function that searches for a
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   336
  \textbf{closed} tour on a $6\times 6$ board. It should use the
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   337
  \texttt{first}-function from (2a) and tries out onward moves according to
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   338
  the \texttt{ordered\_moves} function from (3a). It is more likely to find
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   339
  a solution when started in the middle of the board (that is
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
   340
  position $(dimension / 2, dimension / 2)$). \hfill[1 Mark]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   341
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   342
\item[(3c)] Implement a \texttt{first\_tour\_heuristic} function
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   343
  for boards up to
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   344
  $40\times 40$.  It is the same function as in (3b) but searches for
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   345
  tours (not just closed tours). You have to be careful to write a
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
   346
  tail-recursive function of the \texttt{first\_tour\_heuristic} function
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   347
  otherwise you will get problems with stack-overflows.\\
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
   348
  \mbox{}\hfill[1 Mark]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   349
\end{itemize}  
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   350
\bigskip
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   351
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   352
\noindent
149
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   353
\textbf{Hints:} a useful list function: \texttt{.sortBy} sorts a list
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   354
according to a component given by the function; a function can be
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   355
tested to be tail recursive by annotation \texttt{@tailrec}, which is
3a6f51bc6121 updated
Christian Urban <urbanc@in.tum.de>
parents: 148
diff changeset
   356
made available by importing \texttt{scala.annotation.tailrec}.
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   357
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   358
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
%%% End: