-
Notifications
You must be signed in to change notification settings - Fork 34
/
matrix_path_2-ways_best.pl
executable file
·83 lines (64 loc) · 1.65 KB
/
matrix_path_2-ways_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
77
78
79
80
81
82
83
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 08 August 2016
# Website: https://github.com/trizen
# Visualization for the best minimum path-sum in a matrix.
# Inspired by: https://projecteuler.net/problem=81
# The path moves only right and down.
use 5.010;
use strict;
use warnings;
use List::Util qw(min);
use Time::HiRes qw(sleep);
use Term::ANSIColor qw(colored);
my @matrix = (
[131, 673, 234, 103, 18],
[201, 96, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 956],
[805, 732, 524, 37, 331],
);
my $end = $#matrix;
my @path;
sub draw {
print "\e[H\e[J\e[H";
my @screen = map {
[map { sprintf "%3s", $_ } @{$_}]
} @matrix;
foreach my $path (@path) {
my ($i, $j) = @$path;
$screen[$i][$j] = colored($screen[$i][$j], 'red');
}
foreach my $row (@screen) {
say join(' ', @{$row});
}
sleep(0.05);
}
sub path {
my ($i, $j) = @_;
push @path, [$i, $j];
draw();
pop @path;
if ($i < $end and $j < $end) {
push @path, [$i, $j];
my $sum = $matrix[$i][$j] + min(path($i + 1, $j), path($i, $j + 1));
pop @path;
return $sum;
}
if ($i < $end) {
push @path, [$i, $j];
my $sum = $matrix[$i][$j] + path($i + 1, $j);
pop @path;
return $sum;
}
if ($j < $end) {
push @path, [$i, $j];
my $sum = $matrix[$i][$j] + path($i, $j + 1);
pop @path;
return $sum;
}
$matrix[$i][$j];
}
my $min_pathsum = path(0, 0);
say "\nMinimum path sum is: $min_pathsum\n";