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

In reply to Re: Re: The Matrix by nosbod
in thread The Matrix by nosbod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.