Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a grid-type map (similar to this), and i want to map the quickest path between two coordinates (eg: quickest way to get from 1,8 to 5,8). now, i've got all sections of the grid mapped out in a multidimensional array (named @grid, and used as $grid[x][y], with x as the horizontal coordinate and y as the vertical coordinate), and each array contains a text string. if you're able to go north from that point, the string will contain 'n', if you're able to go south, it would contain 's',and so on for east and west. it can contain any combination of the directions (eg: 'new' means you can go north, east, or west), so i have the following functions already:
sub isNorth { # Passed x & y coords, determines if able to go north my($x,$y) = @_; if($grid[$x][$y] =~ /n/) { return 1 # Can go north, return 1 for true } else { return 0 # Can't go north, return 0 for false } } sub isSouth { # Passed x & y coords, determines if able to go south my($x,$y) = @_; if($grid[$x][$y] =~ /s/) { return 1 # Can go south, return 1 for true } else { return 0 # Can't go south, return 0 for false } } sub isEast { # Passed x & y coords, determines if able to go east my($x,$y) = @_; if($grid[$x][$y] =~ /e/) { return 1 # Can go east, return 1 for true } else { return 0 # Can't go east, return 0 for false } } sub isWest { # Passed x & y coords, determines if able to go west my($x,$y) = @_; if($grid[$x][$y] =~ /w/) { return 1 # Can go west, return 1 for true } else { return 0 # Can't go west, return 0 for false } }
so, with all this, how can I programatically map the shortest path? (or, if there IS no path determinable from the present data, return an appropriate error message?) any & all help appreciated!

Replies are listed 'Best First'.
Re: Mapping paths?...
by merlyn (Sage) on May 18, 2001 at 01:49 UTC
    This code is far too regular. Every subroutine looks like every other subroutine, except for a few parameterized items.

    I don't remember where I first read this (it may have been here from someone like tilly), but the phrase "put the regularity in code, the irregularity in data" comes to mind. The phrase "never type the same thing twice" also springs in here.

    You'll want a data structure that can tell you if you can go a direction. You'll want a way of determining the new cell number if you go that direction. Perhaps some data structure would help, like:

    my %offset = ('n' => [0, 1], 's' => [0, -1], 'e' => [1, 0], 'w' => [-1 +, 0]);
    Then you can do things like:
    my $new_x = $old_x + $offset{$direction}[0]; my $new_y = $old_y + $offset{$direction}[1];
    In fact, I don't even like the regularity in that code. {grin} But I'll probably stop optimizing there.

    -- Randal L. Schwartz, Perl hacker

Re: Mapping paths?...
by no_slogan (Deacon) on May 17, 2001 at 22:23 UTC
    There was just a "shortest path" golf posted. Heh. Seriously, get any introductory book on algorithms and read about Dijkstra's shortest path.
Re: Mapping paths?...
by DrZaius (Monk) on May 17, 2001 at 22:27 UTC
    You should probably use a hash instead of an array for your coordinates. If I remember correctly, if you have an index of 100 as your first entry, perl will create @array[0..99] as well as $array[100].

    Although, you may have all the entries in your grid filled in anyway :)

    I imagine the best way to calculate the path would be determine the slop of the line between the two points and try plotting until you get there.

    If that isn't the best way, I imagine the solution does involve find slopes between two points in fragments.

    good luck.

      If you're using the "Manhattan metric" (each step north, south, east, or west counts as one unit of distance) as seems likely in this case, the slope isn't going to be useful for reducing the distance. OTOH, if the grid is mostly unobstructed, Dijkstra's will try to search outward in a diamond pattern, which could be a lot slower than something that simply tries to connect the dots. That could be important in, for example, a video game AI. Depends on what your exact requirements are.

      BTW, that prohosting website is really and truly ugly. Turn on my cookies and try again? Not likely.

        Heh, thanks :-)

        I implemented the cookie requirement because the program tracks individual browsers to enable submitted tiles to be viewed per poster, and to assign special tile colours to people, and to count the number of different visitors it gets each day.

        If you know of a way to do that without cookies or logging in, you're welcome. The target audience largely has cookies and JavaScript enabled anyway, so I figured I could get away with it. People without cookies would get a Set-cookie header with a different UNIQUE_ID every time they accessed the site (used to be only when they posted), but now, I want to be able to count the number of different browsers that access the site each day, which is a good approximation of the size of the audience, IMHO.

      No, the co-ordinates range from 1 to 30, exactly.