in reply to Re: The Matrix
in thread The Matrix

Yes, you are right, it is possible tojump horizontally multiple steps and vertically by one step.

There is no particular starting and ending point so i guess a genetic algorithm or finite-state automata would be worth looking into

The path that I gave as an example (1,3,1,1,1,1,3,4,3,1,1,1,3,1,1) is a route that uses the first 1 of each row. It is not the best route but is just an example to show a route through the matrix.

Apologies for the messy code. It is heavily commented which makes it look worse. I think this just about works but is not particularly nice. It basically loops recursively starting at the top left and working down the left hand side of the matrix. So, just using the first 4 rows as an example it would loop through the columns in this way:

1,1,1,1
1,1,1,2
1,1,1,3
1,1,1,4
......
1,1,1,21
1,1,2,1
1,1,2,2
1,1,2,3
......
1,1,3,1
1,1,3,2
.......
1,2,1,1
1,2,1,2
......
.......
21,21,21,21

It only stores the route if at every position within the observed route there are 1's

loop(0); # the next section takes each route and counts the number of unique sn +p's for each route # for the num of possible routes through the matrix for $i (0 ... $#finalroute) { $number_of_unique_columns=0; %count=(); # for each step of the particular route we are looking at for (@{$finalroute[$i]}) { $count{$_}++; } # this loop counts the number of keys , therefore gives the number of +columns used in this route thru the matrix for (keys %count) { $number_of_unique_columns++; } $route[$i]=$number_of_unique_columns; } # $column_limit is user input set at the beginning and is the cut-off + for the num of columns used for $i ( 1 ... $column_limit ) { #step the route array which has the number of columns used #for each r +oute. If it equals the $i value (number of #columns) we #are currently looking at then print out the route thru the #matrix th +at is associated with it i.e @{$finalroute[$_]} for (0 ... $#route) { print "@{$finalroute[$_]}\n$route[$_]\n" if $route[$_]==$i; } } sub loop{ my ($row,@array)=@_ ; for (1 ... $#number_of_columns) { push @array,$_ if $matrix[$row][$_] ==1; my $length=@array; my $lengthofmatrix=@matrix; if ($row==$lengthofmatrix) { $finalroute[$block][$y]=[@array] if +$length==$lengthofmatrix; $y++ if $length==$lengthofmatrix; } else { loop($row+1,$block,@array); } pop @array if $matrix[$row][$_]==1; } }
cheers

Replies are listed 'Best First'.
Re: Re: Re: The Matrix
by nosbod (Scribe) on Nov 25, 2002 at 15:42 UTC
    whoops, the sub loop should be
    sub loop{ my ($row,@array)=@_ ; for (1 ... $#number_of_columns) { push @array,$_ if $matrix[$row][$_] ==1; my $length=@array; my $lengthofmatrix=@matrix; if ($row==$lengthofmatrix) { $finalroute[$y]=[@array] if $length==$lengthofmatrix; $y++ if $length==$lengthofmatrix; } else { loop($row+1,@array); } pop @array if $matrix[$row][$_]==1; } }