-
Notifications
You must be signed in to change notification settings - Fork 7
/
083 Path sum four ways.pl
152 lines (114 loc) · 2.96 KB
/
083 Path sum four ways.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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 14 August 2016
# Website: https://github.com/trizen
# https://projecteuler.net/problem=83
# Runtime: 12.369s
# usage: perl problem_083.pl < p083_matrix.txt
use 5.010;
use strict;
use List::Util qw(min max);
my @matrix;
while (<>) {
push @matrix, [split(/,/)];
}
sub draw {
my ($path) = @_;
print "\e[H\e[J\e[H";
my @screen = map {
[map { '.' } @{$_}]
} @matrix;
foreach my $p (@$path) {
my ($i, $j) = @$p;
$screen[$i][$j] = '*';
}
foreach my $row (@screen) {
say join(' ', @{$row});
}
}
my %seen;
sub valid {
not exists $seen{"@_"};
}
my %two_way_cache;
my $end = $#matrix;
sub two_way_path {
my ($i, $j, $k, $l) = @_;
my $key = "$i $j $k $l";
if (exists $two_way_cache{$key}) {
return $two_way_cache{$key};
}
my @paths;
if ($i < $k) {
push @paths, two_way_path($i + 1, $j, $k, $l);
}
if ($j < $l) {
push @paths, two_way_path($i, $j + 1, $k, $l);
}
$two_way_cache{$key} = $matrix[$i][$j] + (min(@paths) || 0);
}
my @stack;
my $sum = 0;
my ($i, $j) = (0, 0);
my $limit = two_way_path(0, 0, $end, $end) + max(map { @$_ } @matrix);
my %min = (sum => 'inf');
while (1) {
undef $seen{"$i $j"};
$sum += $matrix[$i][$j];
my @points;
if ($i >= $end and $j >= $end) {
if ($sum < $min{sum}) {
$min{sum} = $sum;
$min{path} = [keys %seen];
}
@stack ? goto STACK: last;
}
if (not $sum <= $limit or not $sum <= two_way_path(0, 0, $i, $j)) {
goto STACK if @stack;
}
if (not($sum - $matrix[$i][$j] + two_way_path($i, $j, $end, $end) <= $limit)) {
goto STACK if @stack;
}
if ($i > 0 and valid($i - 1, $j)) {
push @points, [$i - 1, $j];
}
if ($j > 0 and valid($i, $j - 1)) {
push @points, [$i, $j - 1];
}
if ($i < $end and valid($i + 1, $j)) {
push @points, [$i + 1, $j];
}
if ($j < $end and valid($i, $j + 1)) {
push @points, [$i, $j + 1];
}
STACK: if (!@points) {
if (@stack) {
my ($s_sum, $s_seen, $s_pos, $s_points) = @{pop @stack};
$sum = $s_sum;
undef %seen;
@seen{@$s_seen} = ();
@points = @$s_points;
($i, $j) = @$s_pos;
}
else {
last;
}
}
my $min = splice(@points, int(rand(@points)), 1);
if (@points and $sum <= $limit and $sum <= two_way_path(0, 0, $i, $j)) {
my @ok = (
grep {
my $s = ($sum + $matrix[$_->[0]][$_->[1]]);
$s <= $limit and $s <= two_way_path(0, 0, $_->[0], $_->[1])
} @points
);
if (@ok) {
push @stack, [$sum, [keys %seen], [$i, $j], \@ok];
}
}
($i, $j) = @$min;
}
my @path = map { [split ' '] } @{$min{path}};
draw(\@path);
say "\nMinimum path-sum is: $min{sum}\n";