-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmatrix_path_3-ways_diagonal_best.pl
executable file
·76 lines (55 loc) · 1.61 KB
/
matrix_path_3-ways_diagonal_best.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 04 August 2017
# https://github.com/trizen
# Find the lowest-cost possible path in a matrix, by starting
# in the top-left corner of the matrix and finishing in the
# bottom-right corner, and only moving up, down, and right.
# Problem closely related to:
# https://projecteuler.net/problem=82
use 5.010;
use strict;
use warnings;
no warnings 'recursion';
use List::Util qw(min);
use Memoize qw(memoize);
memoize('path');
my @matrix = (
[131, 673, 4, 103, 18],
[ 21, 96, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 56],
[805, 732, 524, 37, 331],
);
my $end = $#matrix;
sub path {
my ($i, $j, $last, @path) = @_;
if ($i == $end and $j == $end) {
return ($matrix[$i][$j], @path, $matrix[$i][$j]);
}
elsif ($j > $end) {
return ('inf', @path);
}
my $item = $matrix[$i][$j];
my @paths;
if ($i > 0 and $last ne 'down') {
push @paths, [path($i - 1, $j, 'up', @path, $item)];
}
push @paths, [path($i, $j + 1, 'ok', @path, $item)];
if ($i < $end and $last ne 'up') {
push @paths, [path($i + 1, $j, 'down', @path, $item)];
}
my $min = 'inf';
foreach my $group (@paths) {
my ($sum, @p) = @{$group};
if ($sum < $min) {
$min = $sum;
@path = @p;
}
}
($min + $item, @path);
}
my ($sum, @path) = path(0, 0, 'ok');
say "Cost: $sum"; #=> Cost: 1363
say "Path: [@path]"; #=> Path: [131 21 96 342 4 103 18 150 111 56 331]