Welcome to the Monastery PerlMonks

### Re: Highest total sum path problem

by tybalt89 (Monsignor)
 on Mar 05, 2020 at 18:07 UTC Need Help??

in reply to Highest total sum path problem

Maybe (or maybe not) slight cheating :)

```#!/usr/bin/perl

use strict; # https://perlmonks.org/?node_id=11113757
use warnings;

\$_ = <<END;
1    4    5   -1   9
0    3    0   17  -3
1   10    3    0   3
-8   -3    2    1  -4
0    8    4    0   0
END

my \$destrow = 2;
my \$destcol = 1;
my \$startrow = 4;
my \$startcol = 4;

my @m = map [split], split /\n/;
my @visited;
my \$maxrow = \$#m;
my \$maxcol = \$#{ \$m[0] };
my \$maxscore;
my \$minlength;
my \$best;
\$visited[\$startrow][\$startcol] = 1;

try( \$startrow, \$startcol, 0 );

my @best = split ' ', \$best;
my @values;
print "best path:\n";
for ( my \$i = 0; \$i < @best - 4; \$i += 3 )
{
print directions(@best[\$i, \$i+1, \$i+3, \$i+4]), ' ';
push @values, \$m[\$best[\$i]][\$best[\$i+1]];
}
print "\n= ";
print join '+', @values, \$m[\$best[-3]][\$best[-2]];
print "\n= \$best[-1]\n";

sub try
{
my (\$row, \$col, \$score) = @_[-3 .. -1];
if( \$row == \$destrow && \$col == \$destcol )
{
if( \$maxscore )
{
if( \$score > \$maxscore or
\$score == \$maxscore and \$minlength > @_)
{
\$maxscore = \$score;
\$minlength = @_;
\$best = "@_";
}
}
else
{
\$maxscore = \$score;
\$minlength = @_;
\$best = "@_";
}
return;
}
for my \$r ( 0 .. \$maxrow )
{
if( ++\$visited[\$r][\$col] == 1 )
{
\$m[\$r][\$col] > 0 and
try( @_, \$r, \$col, \$score + \$m[\$r][\$col] );
}
--\$visited[\$r][\$col];
}
for my \$c ( 0 .. \$maxcol )
{
if( ++\$visited[\$row][\$c] == 1 )
{
\$m[\$row][\$c] > 0 and
try( @_, \$row, \$c, \$score + \$m[\$row][\$c] );
}
--\$visited[\$row][\$c];
}
}

sub directions
{
my (\$rowfrom, \$colfrom, \$rowto, \$colto) = @_;
if( \$rowfrom != \$rowto )
{
return +(\$rowfrom < \$rowto ? 'down' : 'up') .
(abs(\$rowfrom - \$rowto) > 1 ? '-slide' : '');
}
else
{
return +(\$colfrom < \$colto ? 'right' : 'left') .
(abs(\$colfrom - \$colto) > 1 ? '-slide' : '');
}
}

Outputs:

```best path:
up-slide down-slide left-slide up-slide right down right-slide down-sl
+ide left up-slide down-slide down-slide left up-slide
= 0+9+3+1+1+4+3+17+1+2+5+3+4+8+10
= 71

"real matrix is 50000*50000" HaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHa

Replies are listed 'Best First'.
Re^2: Highest total sum path problem
by QM (Parson) on Mar 06, 2020 at 17:01 UTC
Shouldn't this be unambiguous? How far is a "slide"?
up-slide down-slide left-slide up-slide right down right-slide down-slide left up-slide down-slide down-slide left up-slide

-QM
--
Quantum Mechanics: The dreams stuff is made of

There were no examples of that in the original post.

Sigh, yet another case of an incomplete spec.

<cough> Can you really blame it on the spec?

Instead you could actually output the distance slid, and move on.

-QM
--
Quantum Mechanics: The dreams stuff is made of

> Shouldn't this be unambiguous?

Well, in this case it is, because the order of the added values is unique.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Re^2: Highest total sum path problem
by LanX (Saint) on Mar 08, 2020 at 00:24 UTC
> "real matrix is 50000*50000" HaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHa

It's not that impossible if the approach was mathematical.

Of course this would require cleanly defined rules and not these "oh good point I forgot to mention that ..." dialogs.°

How this?

• I could imagine proving that a path covering n-x points is maximal possible and exists.
• One would identify the x minimal points to be excluded.
• I expect x to be just the non positive weight cells in most cases anyway.
• There are most likely many equally short paths possible, which could be proven, too.
• Now picking one minimal path should be sufficient.

In short, no need to brute force all possible paths if there are plenty optimal ones and picking one is sufficient.

Of course I might have misunderstood the "rules", but why should I risk to invest more efforts for vain ? ;)

For instance:

• is it really allowed to slide back a way you just slid forward? (Like your first two moves a0-up9-down3)
• is it allowed to slide over positive weights? (Like your third move -left1 ignoring 3 positive cells in between)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) read "I have no real clue what I need"

##### update

Your solution demonstrates my approach pretty well:

• you are visiting all positive cells
• the only non positive one is your starting point
• the shortest path must have the same amount of moves when you avoid revisiting a cell.
• There are certainly many paths meeting that criterion
• most probably automatically constructed from smaller segments like: "finish a column/line before switching to the next one"
> There are certainly many paths meeting that criterion most probably automatically constructed from smaller segments like

OK not trivial, but well studied and becoming easier with growing number of possible moves (i.e. low number of non-positive cells here)

But the general case is NP complete ... hmm ...

... well ... HaHaHaHaHaHaHaHaHaHa .... HaHaHaHaHaHaHaHaHaHa

;)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-06-20 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.