diff options
| author | Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> | 2020-06-13 19:42:54 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-13 19:42:54 +0800 |
| commit | 93e58e2c9e1b29de4a0e6f2562ea67038f5ca810 (patch) | |
| tree | 860f5a0c979d73eec495645d40b34e1ef4aba89b /ch-1.pl | |
| parent | 8694320890bb0076e6a79fe1b10f00a00cd3fbd9 (diff) | |
| download | perlweeklychallenge-club-93e58e2c9e1b29de4a0e6f2562ea67038f5ca810.tar.gz perlweeklychallenge-club-93e58e2c9e1b29de4a0e6f2562ea67038f5ca810.tar.bz2 perlweeklychallenge-club-93e58e2c9e1b29de4a0e6f2562ea67038f5ca810.zip | |
Add files via upload
Diffstat (limited to 'ch-1.pl')
| -rw-r--r-- | ch-1.pl | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/ch-1.pl b/ch-1.pl new file mode 100644 index 0000000000..fa13f555f8 --- /dev/null +++ b/ch-1.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +use strict; +use Data::Dumper; +use List::Util qw/sum/; + +# $steps : a string looks like DDDDRRRR, DDRRRR, RDRDRD... + +# my @ms = ([1,2,3], [4,5,6], [7,8,9]); # ms = shorthand of matrixrows +# ans = 21 +# my @ms = ([1, 4, 7], [2, 0, 8], [3, 6, 9]); +# ans = 21 + my @ms = ([1, 4, 7, 12], [2, 0, 8, 9], [3, 6, 9, 8]); +# ans = 26 + +my @min_path; + +my $M = $#ms+1; +my $N = $#{$ms[0]} + 1; +my $totsteps = $M+$N-2; + +# a starting minimum +my $refmin = sum @{$ms[0]}; +for (1..$M-1) { + $refmin += ${$ms[$_]}[$N-1]; +} + +sub go { + my $steps = $_[0]; + my $hardship = $_[1]; + $hardship += ${$ms[otimes($steps, 'D')]}[otimes($steps,'R')]; + myseek($steps, $hardship) if ($hardship <= $refmin); +} + + + +sub myseek { + my $steps = $_[0]; + my $hardship = $_[1]; + # print $steps, " ", $hardship, "\n"; for testing + if ( (length $steps) < $totsteps ) { + go($steps.'D', $hardship) if $M-1 > otimes($steps, 'D'); + go($steps.'R', $hardship) if $N-1 > otimes($steps, 'R'); + } + elsif ( $hardship <= $refmin) { + @min_path = tour($steps); + $refmin = $hardship; + } +} + + +sub otimes { + my $t = 0; + for my $x (split //, $_[0]) { + if ($x eq $_[1]) { + $t++; + } + } + return $t; +} + +sub tour { + my $steps = $_[0]; + my @_a = ( ${ms[0]}[0] ); + my $x = 0; + my $y = 0; + my @ways = split //, $steps; + for my $w (@ways) { + if ($w eq 'D') {$y++;} + if ($w eq 'R') {$x++;} + push @_a, ${ms[$y]}[$x]; + } +# push @_a, ${ms[$M-1]}[$N-1]; + return @_a; +} + + +myseek('',${$ms[0]}[0]); + +print $refmin, "\n"; +print join " -> ", @min_path; +print "\n"; |
