-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-2.pl
executable file
·158 lines (124 loc) · 4.46 KB
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/perl -s
use v5.16;
use Test2::V0;
use Graph;
use List::Util 'reduce';
use Math::Prime::Util qw(forperm forsetproduct vecsum);
use experimental 'signatures';
our ($examples, $tests, $start);
$start //= 'a8';
@ARGV = qw(b1 a2 b2 b3 c4 e6) if $examples;
run_tests() if $tests; # does not return
die <<EOS unless @ARGV;
usage: $0 [-examples] [-tests] [-start=pos] [t1 ...]
-examples
run the examples from the challenge
-tests
run some tests
-start=pos
use pos as the starting square for the knight. Default: a8
t1 ...
positions of treasures on the board in chess notation (a1 ... h8)
Solving the example:
$0 b1 a2 b2 b3 c4 e6
or simply:
$0 -examples
Output is one line for each path from the starting square / previous
treasure square to the next treasure square.
EOS
### Input and Output
say join '->', @$_ for @{adventure_of_knight($start, @ARGV)};
### Implementation
# The task can be divided into four subtasks:
#
# 1) Build the knight's graph. See
# https://en.wikipedia.org/wiki/Knight%27s_graph
#
# 2) Find the shortest paths between the start and all treasure squares
# within the knight's graph and build a weighted "treasure graph" out
# of it. See https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
#
# 3) Solve the travelling salesman problem in the "treasure graph". See
# https://en.wikipedia.org/wiki/Travelling_salesman_problem
#
# 4) Present the solution on the board. This is probably the most
# laborious part and has been left out here.
# Solve the task: subtasks 1) to 3).
sub adventure_of_knight ($start, @treasures) {
min_hamiltonian($start,
treasure_graph(knights_graph(), $start, @treasures));
}
# Build the knight's graph.
sub knights_graph {
my $g = Graph::Undirected->new;
forsetproduct {
$g->add_edge($_[0] . $_[1], $_) for knights_moves(@_);
} ['a' .. 'h'], [1 .. 8];
$g;
}
# Find all possible knight's moves going two squares ascending. No need
# to consider the opposite directions by symmetry. Use "character
# arithmetics" for the alphabetic column identifiers.
sub knights_moves (@sq) {
map $_->[0] . $_->[1],
grep $_->[0] ge 'a' && $_->[0] le 'h'
&& $_->[1] > 0 && $_->[1] <= 8,
map [chr(ord($sq[0]) + $_->[0]), $sq[1] + $_->[1]],
[2, -1], [2, 1], [-1, 2], [1, 2];
}
# Find the shortest paths between the start square and all treasure
# squares in the knight's graph using Dijkstra's algorithm. The result
# is a directed graph ("treasure graph") where the edges are tagged with
# the corresponding directed paths in the knight's graph and weighted
# with the paths' lengths.
sub treasure_graph ($g, $start, @treasures) {
# Representation of the treasure graph as HoHoA:
# origin, target, path.
my %paths;
# One-way from the start square.
$paths{$start}{$_} = [$g->SP_Dijkstra($start, $_)] for @treasures;
# Two-way between the treasure squares.
while (my $this = shift @treasures) {
for my $that (@treasures) {
my @path = $g->SP_Dijkstra($this, $that);
$paths{$this}{$that} = \@path;
$paths{$that}{$this} = [reverse @path];
}
}
\%paths;
}
# Find a minimum weighted Hamiltonian path in the treasure graph from
# the start square with the assigned path's length as weight. By
# construction, every path from the starting square visiting any
# permutation of the treasure squares is valid and Hamiltonian.
# Adding directed, zero-weighted edges between all treasure squares and
# the start square would turn this into an equivalent asymmetric TSP -
# just to spot the complexity of the task. Not attempting any
# optimizations.
sub min_hamiltonian ($start, $treasure) {
my @treasures = grep {$_ ne $start} keys %$treasure;
my ($minlen, $shortest) = 'inf';
# Try all permutations of the treasure squares for the minimum path.
forperm {
my @paths;
# Abuse "reduce" as a sliding window.
reduce {
push @paths, $treasure->{$a}{$b};
$b;
} $start, @treasures[@_];
# Record a new minimum.
if ((my $len = vecsum map scalar @$_, @paths) < $minlen) {
$shortest = \@paths;
$minlen = $len;
}
} @treasures;
$shortest;
}
### Examples and tests
sub run_tests {
is adventure_of_knight(qw(a1 d8 f7 h6 g4 e3 c2)),
[[qw(a1 c2)], [qw(c2 e3)], [qw(e3 g4)], [qw(g4 h6)],
[qw(h6 f7)], [qw(f7 d8)]], 'lined up';
done_testing;
exit;
}