in reply to The Matrix

Just on the top of it, it does sound a bit homework-ish, but it interested me none the less.

You seem to indicate that you can jump multiple steps in the horizontal, but can you also jump multiple steps vertically, or are you limited to vertical stepping by one?

Is there one particular starting and ending point, such as the upper-left and bottom-right corners, or can you start from any point on the upper row as long as you end up on the bottom row? Assuming it is, this problem sounds as if it would almost reduce to a standard maze problem, for which there are numerous solutions available. If the latter, then I see the problem getting much more complex and time-consuming, since you have to consider starting from each location on the top row, at which point I would start looking for a more unusual solution, such as one using a genetic algorithm or finite-state automata. (I once used the latter in solving a maze for a class-quite interesting.)

I can't think of any modules off-handedly that would help (although if you have to get into one of those more unusual solutions I mentioned, perhaps something in the AI section of CPAN or dealing with discrete finite automata might be helpful).

As to thinking-caps, the reason I asked above about multiple vertical steps was because if so, you could rotate a copy of the array around the axis between upper-left and lower-right corners to aid in scanning for vertical jump possibilities.

I don't know if that helps any, but it does sound like an interesting problem, and I do hope you have the opportunity to post more regarding the problem.

Update: I must agree with Abigail-II-can you please clairify the notation you are using for your path? I could not make sense of the path at one point, and was unsure if I was misunderstanding or if there was an error in the path. Thanks.

Update: Appologies to Abigail-II and others for introducing something more complicated than necessary for solving a problem. My reason for suggesting such things was a weakness in the arena of graph problems.

Replies are listed 'Best First'.
Re: The Matrix
by Abigail-II (Bishop) on Nov 25, 2002 at 13:53 UTC
    If you can start on any top row location, and finish on any bottow row location, the problems doesn't at all become much more complex and time consuming. Just add two more nodes to the maze: a start node with exits to all the valid top row locations, and a finish node, with entries from all the valid bottow row locations, and you have a "standard maze" again, with 1 start, and 1 end position.

    Now, why on earth you are referring to the AI or finite automata to solve this problem is beyond me. It's a graph problem, all you want is a path from one end to the other, and if there are multiple paths, you want to find a minimal one using some measurement.

    Abigail

Re: Re: The Matrix
by AcidHawk (Vicar) on Nov 25, 2002 at 13:55 UTC

    There is also no mention of wether or not the 1's need to be adjacent..

    So you seem able to do this...

    1 1 0 0 0 0 0
    0 0 0 0 0 1 1
    0 1 1 0 0 0 0
    0 0 0 0 0 1 0
    0 0 1 0 1 0 0

    Where the shortest path would seem to be 16263.

    Update: nosbod clarified that the shortest path in the above example would rather be 26365 as you want to jump the least number of columns and not the LEFTMOST route...which would suggest the you could start anywhere on the top row..?

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      that would be a valid route, yes
Re: Re: The Matrix
by nosbod (Scribe) on Nov 25, 2002 at 15:37 UTC
    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
      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; } }
Re: Re: The Matrix
by thewalledcity (Friar) on Nov 25, 2002 at 19:33 UTC
    Umm, except DFA stands for Deterministic Finite Automata. The module author got it wrong.