cws/cw02.tex
author Christian Urban <urbanc@in.tum.de>
Mon, 14 Nov 2016 13:10:51 +0000
changeset 48 7a6a75ea9738
parent 46 48d2cbe8ee5e
child 50 9891c9fac37e
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}
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
     4
\usepackage{../style}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
\setchessboard{smallboard,
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
     9
               zero,
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
               showmover=false,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
               boardfontencoding=LSBC4,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
               hlabelformat=\arabic{ranklabel},
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
               vlabelformat=\arabic{filelabel}}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    15
\mbox{}\\[-18mm]\mbox{}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    17
\section*{Coursework 7 (Scala, Knight's Tour)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    19
This coursework is about searching and backtracking, and worth
42
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    20
10\%. The first part is due on 23 November at 11pm; the second, more
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    21
advanced part, is due on 30 November at 11pm. You are asked to
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    22
implement Scala programs that solve various versions of the
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    23
\textit{Knight's Tour Problem} on a chessboard. Make sure the files
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    24
you submit can be processed by just calling \texttt{scala
a5106bc13db6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    25
  <<filename.scala>>}.
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    26
 
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    27
\subsection*{Disclaimer}
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    28
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    29
It should be understood that the work you submit represents
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    30
your own effort. You have not copied from anyone else. An
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    31
exception is the Scala code I showed during the lectures or
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    32
uploaded to KEATS, which you can freely use.\medskip
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    33
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    34
\subsection*{Background}
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    35
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    36
The \textit{Knight's Tour Problem} is about finding a tour such that
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    37
the knight visits every field on an $n\times n$ chessboard once. For
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    38
example on a $5\times 5$ chessboard, a knight's tour is:
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    39
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    40
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    41
\chessboard[maxfield=d4, 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    42
            pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    43
            text = \small 24, markfield=Z4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    44
            text = \small 11, markfield=a4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    45
            text = \small  6, markfield=b4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    46
            text = \small 17, markfield=c4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    47
            text = \small  0, markfield=d4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    48
            text = \small 19, markfield=Z3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    49
            text = \small 16, markfield=a3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    50
            text = \small 23, markfield=b3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    51
            text = \small 12, markfield=c3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    52
            text = \small  7, markfield=d3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    53
            text = \small 10, markfield=Z2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    54
            text = \small  5, markfield=a2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    55
            text = \small 18, markfield=b2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    56
            text = \small  1, markfield=c2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    57
            text = \small 22, markfield=d2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    58
            text = \small 15, markfield=Z1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    59
            text = \small 20, markfield=a1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    60
            text = \small  3, markfield=b1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    61
            text = \small  8, markfield=c1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    62
            text = \small 13, markfield=d1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    63
            text = \small  4, markfield=Z0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    64
            text = \small  9, markfield=a0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    65
            text = \small 14, markfield=b0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    66
            text = \small 21, markfield=c0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    67
            text = \small  2, markfield=d0
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    68
           ]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    69
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    70
\noindent
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    71
The tour starts in the right-upper corner, then moves to field
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    72
$(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
    73
$2\times 2$, $3\times 3$ and $4\times 4$ chessboards, but for every
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    74
bigger board there is.
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    75
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    76
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
    77
is within a knight's move to the beginning of the tour. So the above
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    78
knight's tour is \underline{not} closed (it is open) because the last
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    79
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
    80
$(4, 4)$. It turns out there is no closed knight's tour on a $5\times
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
    81
5$ board. But there are on a $6\times 6$ board and bigger, for example
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
\chessboard[maxfield=e5, 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
            pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    85
            text = \small 10, markfield=Z5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    86
            text = \small  5, markfield=a5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    87
            text = \small 18, markfield=b5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    88
            text = \small 25, markfield=c5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    89
            text = \small 16, markfield=d5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    90
            text = \small  7, markfield=e5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    91
            text = \small 31, markfield=Z4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    92
            text = \small 26, markfield=a4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    93
            text = \small  9, markfield=b4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    94
            text = \small  6, markfield=c4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    95
            text = \small 19, markfield=d4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    96
            text = \small 24, markfield=e4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    97
            % 4  11  30  17   8  15 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    98
            text = \small  4, markfield=Z3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    99
            text = \small 11, markfield=a3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   100
            text = \small 30, markfield=b3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   101
            text = \small 17, markfield=c3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   102
            text = \small  8, markfield=d3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   103
            text = \small 15, markfield=e3,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   104
            %29  32  27   0  23  20 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   105
            text = \small 29, markfield=Z2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   106
            text = \small 32, markfield=a2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   107
            text = \small 27, markfield=b2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   108
            text = \small  0, markfield=c2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   109
            text = \small 23, markfield=d2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   110
            text = \small 20, markfield=e2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   111
            %12   3  34  21  14   1 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   112
            text = \small 12, markfield=Z1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   113
            text = \small  3, markfield=a1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   114
            text = \small 34, markfield=b1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   115
            text = \small 21, markfield=c1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   116
            text = \small 14, markfield=d1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   117
            text = \small  1, markfield=e1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   118
            %33  28  13   2  35  22 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   119
            text = \small 33, markfield=Z0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   120
            text = \small 28, markfield=a0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   121
            text = \small 13, markfield=b0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   122
            text = \small  2, markfield=c0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   123
            text = \small 35, markfield=d0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   124
            text = \small 22, markfield=e0,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   125
            vlabel=false,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   126
            hlabel=false
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
           ]
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   129
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
\noindent
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   131
where the 35th move can join up again with the 0th move.
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   132
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   133
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
   134
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
   135
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
   136
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   137
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   138
\chessboard[maxfield=g7,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   139
            color=blue!50,
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
            linewidth=0.2em,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
            shortenstart=0.5ex,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
            shortenend=0.5ex,
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
            markstyle=cross,
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   144
            markfields={a4, c4, Z3, d3, Z1, d1, a0, c0},
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
            color=red!50,
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   146
            markfields={f5, e6},
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   147
            setpieces={Ng7, Nb2}]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   148
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   149
\subsection*{Part 1 (6 Marks)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   150
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   151
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
   152
dimension of the board can be changed.  Therefore most functions will
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   153
take the dimension as an argument.  The fun with this problem is that
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   154
even for small chessbord dimensions it has already an incredably large
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   155
search space---finding a tour is like finding a needle in a
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   156
haystack. In the first task we want to see far we get with
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   157
exhaustively exploring the complete search space for small
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   158
chessboards.\medskip
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   160
\noindent
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   161
Let us first fix the basic datastructures for the implementation.  The
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   162
board dimension is an integer (we will never go boyond board sizes of
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   163
$100 \times 100$).  A \emph{position} (or field) on the chessboard is
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   164
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
   165
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
   166
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
   167
example the path for the $5\times 5$ chessboard above is represented
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   168
by
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   170
\[
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   171
\texttt{List($\underbrace{\texttt{(0, 4)}}_{24}$,
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   172
  $\underbrace{\texttt{(2, 3)}}_{23}$, ...,
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   173
  $\underbrace{\texttt{(3, 2)}}_1$, $\underbrace{\texttt{(4, 4)}}_0$)}
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
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   176
\noindent
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   177
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
   178
\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
   179
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
   180
knight moves (see above for the rules).
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   183
\subsubsection*{Tasks (file knight1.scala)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   184
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   185
\begin{itemize}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   186
\item[(1a)] Implement a is-legal-move function that takes a dimension, a path
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   187
and a position as argument and tests whether the position is inside
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   188
the board and not yet element in the path. \hfill[1 Mark]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   189
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   190
\item[(1b)] Implement a legal-moves function that calculates for a
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   191
  position all legal onward moves. If the onward moves are
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   192
  placed on a circle, you should produce them starting from
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   193
  ``12-oclock'' following in clockwise order.  For example on an
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   194
  $8\times 8$ board for a knight on position $(2, 2)$ and otherwise
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   195
  empty board, the legal-moves function should produce the onward
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   196
  positions
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   198
  \begin{center}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   199
  \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
   200
  \end{center}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   201
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   202
  in this order.  If the board is not empty, then maybe some of the
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   203
  moves need to be filtered out from this list.  For a knight on field
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   204
  $(7, 7)$ and an empty board, the legal moves are
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   205
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   206
  \begin{center}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   207
  \texttt{List((6,5), (5,6))}
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   208
  \end{center}
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   209
  \mbox{}\hfill[1 Mark]
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
\item[(1c)] Implement two recursive functions (count-tours and
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   212
  enum-tours). They each take a dimension and a path as
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   213
  arguments. They exhaustively search for \underline{\bf open} tours
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   214
  starting from the given path. The first function counts all possible
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   215
  open tours (there can be none for certain board sizes) and the second
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   216
  collects all open tours in a list of paths.\hfill[2 Marks]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   217
\end{itemize}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   219
\noindent \textbf{Test data:} For the marking, the functions in (1c)
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   220
will be called with board sizes up to $5 \times 5$. If you only search
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   221
for open tours on $5 \times 5$ board starting from field $(0, 0)$,
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   222
there are 304 of them. If you try out every field of a $5 \times
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   223
5$-board as a starting field and add up all open tours, you obtain
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   224
1728. A $6\times 6$ board is already too large to be searched
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   225
exhaustively.\footnote{For your interest, the number of open tours on
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   226
  $6\times 6$, $7\times 7$ and $8\times 8$ are 6637920, 165575218320,
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   227
  19591828170979904, respectively.}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   228
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   229
\subsubsection*{Tasks (file knight2.scala)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   230
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   231
\begin{itemize}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   232
\item[(2a)] Implement a first-function. This function takes a list of
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   233
  positions and a function $f$ as arguments. The function $f$ takes a
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   234
  position as argument and produces an optional path. The idea behind
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   235
  the first-function is as follows:
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   236
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   237
  \[
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   238
  \begin{array}{lcl}
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   239
  \textit{first}(\texttt{Nil}, f) & \dn & \texttt{None}\\  
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   240
  \textit{first}(x\!::\!xs, f) & \dn & \begin{cases}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   241
    f(x) & \textit{if}\;f(x) \not=\texttt{None}\\
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   242
    \textit{first}(xs, f) & \textit{otherwise}\\
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   243
                              \end{cases}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   244
  \end{array}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   245
  \]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   246
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   247
  \noindent That is, we want to find the first position where the
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   248
  result of $f$ is not \texttt{None}.\newline\mbox{}\hfill[1 Mark]
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   249
  
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   250
\item[(2b)] Implement a first-tour function. Using the first-function
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   251
  from (2a), search recursively for an open tour.  As there might not
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   252
  be such a tour at all, the first-tour function needs to return an
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   253
  \texttt{Option[Path]}.\hfill[2 Marks]
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   254
\end{itemize}
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   255
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   256
\noindent
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   257
\textbf{Testing} The first tour function will be called with board
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   258
sizes of up to $8 \times 8$. 
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   261
\subsection*{Part 2 (4 Marks)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   262
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   263
As you should have seen in Part 1, a naive search for open tours
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   264
beyond $8 \times 8$ boards and also for searching for closed tours
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   265
takes too much time. There is a heuristic (called Warnsdorf's rule)
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   266
that can speed up finding a tour. This heuristice states that a knight
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   267
is moved so that it always proceeds to the square from which the
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   268
knight will have the \underline{fewest} onward moves.  For example for
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   269
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
   270
onward moves, namely 2.
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   271
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   272
\chessboard[maxfield=g7,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   273
            pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text},
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   274
            text = \small 3, markfield=Z5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   275
            text = \small 7, markfield=b5,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   276
            text = \small 7, markfield=c4,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   277
            text = \small 7, markfield=c2,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   278
            text = \small 5, markfield=b1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   279
            text = \small 2, markfield=Z1,
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   280
            setpieces={Na3}]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   281
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   282
\noindent
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   283
Warnsdorf's rule states that the moves on the board above sould be
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   284
tried out in the order
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   285
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   286
\[
46
48d2cbe8ee5e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   287
(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
   288
\]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   289
46
48d2cbe8ee5e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   290
\noindent
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   291
Whenever there are ties, the correspoding onward moves can be in any
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   292
order.  When calculating the number of onward moves for each field, we
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   293
do not count moves that revisit any field already visited.
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   294
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   295
\subsubsection*{Tasks (file knight3.scala)}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   296
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   297
\begin{itemize}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   298
\item[(3a)] orderered-moves
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   299
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   300
\item[(3b)] first-closed tour heuristics; up to $6\times 6$
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   301
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   302
\item[(3c)] first tour heuristics; up to $50\times 50$
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   303
\end{itemize}  
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
%%% End: