The Knight's Tour in Chess – Implementing a Heuristic

advertisement
WUSS 2015
The Knight’s Tour in Chess –
Implementing
a Heuristic
Solution
The Knight’s
Tour in
Chess – Implementing a Heuristic Solution
John R Gerlach
John R Gerlach
Abstract
Knight’s Tour: A sequence of moves on a
chess board such that a knight visits each
square only once.
Heuristic Solution: Always move the
knight to an adjacent, unvisited square
with minimal degree. – H.C. Warnsdorff
1. Knight Moves
2. Heuristic Method
Identify the number of possible moves from
every square.
Find a tour
• Move the knight to viable square
• Revise chess board
• Update knight moves data set
• Continue until completed tour
Make sure the knight does not fall off the board.
3. Identify Knight Moves
a8  b6, c7 (Degree 2)
c6  b8, d8, a7, e7, a5, e5, b4, e4 (Degree 8)
data knightmoves;
array board{8,8} m11-m18 m21-m28 m31-m38 m41-m48
m51-m58 m61-m68 m71-m78 m81-m88;
do r = 1 to 8;
do c = 1 to 8;
counter = 0;
do step1 = -2,-1,1,2;
do step2 = -2,-1,1,2;
if (abs(step1) ne abs(step2))
then do;
if 1 le (r+step1) le 8
and 1 le (c+step2) le 8
then counter+1;
end;
end;
end;
board{r,c} = counter;
end;
end;
drop r c step1 step2 counter;
run;
4. Knight Moves Data Set
2
3
4
4
4
4
3
2
3
4
6
6
6
6
4
3
4
6
8
8
8
8
6
4
4
6
8
8
8
8
6
4
4
6
8
8
8
8
6
4
4
6
8
8
8
8
6
4
3
4
6
6
6
6
4
3
2
3
4
4
4
4
3
2
5. SAS Solution
%macro Warnsdorff;
%do r = 1 %to 8;
%do c = 1 %to 8;
< Find Knight’s Tour >
< Display Chess Board >
%end;
%end;
%mend Warnsdorff;
<Section><Paper>
The Knight’s Tour in Chess – Implementing a Heuristic Solution
John R Gerlach
7. Find Next Move
6. Find Knight’s Tour
data solution;
retain r &r. c &c.;
retain s11-s18 s21-s28 s31-s38
s41-s48 s51-s58 s61-s68
s71-s78 s81-s88;
array board{8,8} s11-s18 s21-s28
s31-s38 s41-s48
s51-s58 s61-s68
s71-s78 s81-s88;
array moves{8,8} m11-m18 m21-m28
m31-m38 m41-m48
m51-m58 m61-m68
m71-m78 m81-m88;
set knightmoves;
board{r,c} = 1;
do position = 2 to 64;
nxtr
= 0;
nxtc
= 0;
pmoves = 9;
do step1 = -2,-1,1,2;
do step2 = -2,-1,1,2;
< Find Next move >
end;
end;
< Update Metadata >
end;
run;
•
•
•
•
Discern valid step values (e.g. -2,1)
Find valid row and column (Stay on the board)
Find viable square (Unused square)
Take next step.
if abs(step1) ne abs(step2)
then do;
if ( 1 le (r+step1) le 8 )
and ( 1 le (c+step2) le 8 )
and board(r+step1,c+step2) eq .
then do;
if moves{r+step1,c+step2}
lt pmoves
then do;
nxtr
= r + step1;
nxtc
= c + step2;
pmoves = moves{r+step1,
c+step2};
end;
end;
end;
8. Update Metadata
• Update chess board
• Update knight moves data set
if 1 le nxtr le 8
and 1 le nxtc le 8
then do;
r = nxtr;
c = nxtc;
board{r,c} = position;
do step1 = -2,-1,1,2;
do step2 = -2,-1,1,2;
if abs(step1) ne abs(step2)
and (1 le (r+step1) le 8)
and (1 le (c+step2) le 8)
then moves{r+step1,c+step2}=
moves{r+step1,c+step2}-1;
end;
end;
end;
The Knight’s Tour in Chess – <Section><Paper>
The Knight’s Tour in Chess – Implementing a Heuristic Solution
Implementing
a Heuristic
Solution
The Knight’s
Tour in
Chess – Implementing a Heuristic Solution
John
R RGerlach
John R Gerlach
John
Gerlach
9. Update Metadata – Before and After
MOVES
Before
2
3
4
4
4
4
3
2
3
4
6
6
6
6
4
3
4
6
8
8
8
8
6
4
4
6
8
8
8
8
6
4
4
6
8
8
8
8
6
4
4
6
8
8
8
8
6
4
BOARD
Knight a8 to c7
3
4
6
6
6
6
4
3
2
3
4
4
4
4
3
2
1
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
11. Display Chess Board
MOVES
After
.
.
.
.
.
.
.
.
1
3
3
4
4
4
3
2
3
4
6
5
6
6
4
3
4
6
8
8
8
8
6
4
4
6
8
7
8
8
6
4
3
6
7
8
8
8
6
4
4
6
8
8
8
8
6
4
3
4
6
6
6
6
4
3
2
3
4
4
4
4
3
2
• Number of possible knight moves changes at a8, a6, e8, e6, b5, d5.
10. Completed Tour?
• Create the macro variable SOLVED.
• Boolean expression assigns the values {0,1}.
data _null_;
array board{8,8} s11-s18 s21-s28 s31-s38 s41-s48
s51-s58 s61-s68 s71-s78 s81-s88;
set solution;
call symput(‘solved’
left(put(n(of board{*}) eq 64,1.)));
run;
%if %solved.
%then %do;
%ShowBoard(dsn
= solution,
elements = s11-s18 s21-s28
s31-s38 s41-s48
s51-s58 s61-s68
s71-s78 s81-s88,
title
= Chess Board -- Run #&run.);
%end;
12. Display Chess Board
%macro ShowBoard(dsn
= solution,
elements = s11-s18 s21-s28 s31-s38 s41-s48
s51-s58 s61-s68 s71-s78 s81-s88);
data board(keep=c1-c8);
array board{8,8} &elements.;
array cols{8} c1-c8;
set &dsn.;
do r = 1 to 8;
do c = 1 to 8;
cols{c} = board{r,c};
end;
output;
end;
run;
The Knight’s Tour in Chess – <Section><Paper>
The Knight’s Tour in Chess – Implementing a Heuristic Solution
Implementing
a Heuristic
Solution
The Knight’s
Tour in
Chess – Implementing a Heuristic Solution
John
R RGerlach
John R Gerlach
John
Gerlach
13. Display Chess Board (cont.)
proc report data=board nowindows headskip;
columns c1-c8;
define c1 / display width=3 '';
define c2 / display width=3 '';
define c3 / display width=3 '';
define c4 / display width=3 '';
define c5 / display width=3 '';
define c6 / display width=3 '';
define c7 / display width=3 '';
define c8 / display width=3 '';
format c1-c8 best3.1;
title2 "&title.";
run;
%mend ShowBoard;
%ShowBoard Macro:
• Converts data set from 1 observation to 8 rows / 8 columns.
• Uses REPORT procedure to render the chess board.
14. Display Chess Board – Knight’s Tour #25
20
33
16
1
26
31
14
61
17
2
19
32
15
60
27
30
34
21
38
25
36
29
62
13
3
18
35
46
59
64
53
28
22
39
24
37
52
55
12
63
7
4
47
42
45
58
51
54
40
23
6
9
56
49
44
11
5
8
41
48
43
10
57
50
15. Making a Wrong Turn
Using First Instance of
Minimal Degree
Tour #23
35
20
23
6
41
10
25
8
22
5
34
45
24
7
42
11
19
36
21
40
43
56
9
26
4
33
44
57
46
51
12
55
37
18
39
50
.
54
27
52
32
3
58
47
60
49
.
13
17
38
1
30
15
.
53
28
Using Last Instance of
Minimal Degree
Tour #11
2
31
16
59
48
29
14
.
2
17
28
23
4
19
30
33
27
24
3
18
29
32
5
20
16
1
26
51
22
61
34
31
25
52
55
62
.
50
21
6
56
15
.
49
54
37
60
35
45
48
53
38
59
40
7
10
14
57
46
43
12
9
36
41
47
44
13
58
39
42
11
8
• The Set Union of First & Last Instances of Minimal Degree produces a complete set
of knight tours.
The Knight’s Tour in Chess – <Section><Paper>
The Knight’s Tour in Chess – Implementing a Heuristic Solution
Implementing
a Heuristic
Solution
The Knight’s
Tour in
Chess – Implementing a Heuristic Solution
John
R RGerlach
John R Gerlach
John
Gerlach
Knight’s Tour #1
1
30
15
36
43
28
13
38
16
35
44
29
14
37
64
27
31
2
33
42
61
46
39
12
34
17
60
45
40
53
26
63
3
32
41
54
47
62
11
56
18
49
20
59
52
55
8
25
21
4
51
48
23
6
57
10
50
19
22
5
58
9
24
7
4
7
2
23
36
9
40
49
1
22
5
8
41
50
37
10
Knight’s Tour #4
20
17
34
3
22
7
44
5
33
2
21
18
43
4
23
8
16
19
42
35
24
59
6
45
1
32
25
54
49
46
9
60
26
15
36
41
62
53
58
47
31
38
29
50
55
48
61
10
Knight’s Tour #3
Knight’s Tour #2
6
3
24
35
26
39
48
51
21
34
27
42
55
52
11
38
28
17
54
25
58
43
60
47
33
20
31
44
53
56
63
12
16
29
18
57
14
59
46
61
19
32
15
30
45
62
13
64
Knight’s Tour #5
14
27
40
37
12
63
52
57
39
30
13
28
51
56
11
64
39
30
13
28
45
48
11
58
14
27
40
37
12
59
50
47
31
38
29
44
49
46
57
10
26
15
36
41
60
55
62
51
1
32
25
54
43
52
9
56
16
19
42
35
24
61
6
63
23
2
21
18
25
4
41
60
20
17
24
3
42
61
26
5
1
22
19
48
27
40
59
64
16
29
46
43
62
57
6
39
33
12
49
28
47
44
63
58
30
15
32
45
56
53
38
7
11
34
13
50
9
36
55
52
14
31
10
35
54
51
8
37
Knight’s Tour #11
33
2
21
18
53
4
23
8
20
17
34
3
22
7
64
5
2 23
4 19 44 39 14 17
5 20
1 40 15 18 45 38
24
3 22 43 48 55 16 13
21
6 49 56 41 46 37 54
50 25 42 47 62 53 12 33
7 28 59 52 57 34 61 36
26 51 30
9 60 63 32 11
29
8 27 58 31 10 35 64
• Corrected using first instance of minimal
degree.
The Knight’s Tour in Chess – <Section><Paper>
The Knight’s Tour in Chess – Implementing a Heuristic Solution
Implementing
a Heuristic
Solution
The Knight’s
Tour in
Chess – Implementing a Heuristic Solution
John
R RGerlach
John R Gerlach
John
Gerlach
Knight’s Tour #19
5
8
3
24
49
10
47
62
2
23
6
9
54
63
50
11
7
4
1
56
25
48
61
46
22
33
26
53
64
55
12
51
27
18
57
34
41
52
45
60
32
21
30
37
58
35
40
13
17
28
19
42
15
38
59
44
20
31
16
29
36
43
14
39
29 26
7 46 21 24
5
2
8 45 28 25
6
3 20 23
27 30 59 42 47 22
1
4
44
9 48 61 64 55 40 19
31 60 43 58 41 62 15 56
10 49 34 63 54 57 18 39
35 32 51 12 37 16 53 14
50 11 36 33 52 13 38 17
• Corrected using last instance of minimal
degree.
Knight’s Tour #40
36
25
38
7
44
23
40
5
33
8
35
24
39
6
45
22
26
37
32
43
46
61
4
41
9
34
47
62
51
42
21
54
48
27
50
31
60
53
64
3
13
10
59
52
63
18
55
20
Knight’s Tour #35
Knight’s Tour #23
Knight’s Tour #64
28
49
12
15
30
57
2
17
11
14
29
58
1
16
19
56
7
10
41
38
57
12
43
46
40
35
8
11
42
45
56
13
9
6
39
58
37
52
47
44
34
59
36
53
48
63
14
55
5
20
49
64
61
54
51
24
30
33
60
21
50
25
62
15
21
34
17
2
27
32
15
50
18
3
20
33
16
49
28
31
35
22
53
26
1
30
51
14
4
19
36
55
52
57
48
29
23
54
25
58
37
64
13
60
8
5
42
63
56
59
38
47
43
24
7
10
45
40
61
12
Thank you!
19
4
31
28
17
2
23
26
32
29
18
3
22
27
16
1
Welcome to San Diego!
Thanks to Dataceutics, Inc.
John R. Gerlach
gerlachj@dataceutics.com
6
9
44
41
62
11
46
39
Download