in reply to spiral path traversal for a grid
EDIT: Whose doesn't work for 4 rows, ikegami? I tested mine with a number of different configurations, including several with 4 rows, and it seemed to work just fine.use strict; use warnings; my (@grid, $top, $bottom, $left, $right); while (<DATA>) { chomp; push @grid, [split / /]; } $top = 0; $bottom = $#grid; $left = 0; $right = $#{$grid[0]}; while (1) { print $_ for @{$grid[$top]}[$left..$right]; last if ++$top > $bottom; print $grid[$_][$right] for $top..$bottom; last if --$right < $left; print $_ for @{$grid[$bottom]}[reverse $left..$right]; last if --$bottom < $top; print $grid[$_][$left] for reverse $top..$bottom; last if ++$left > $right; } __DATA__ a b c d e f g h i j k l m n o p q r s t u
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: spiral path traversal for a grid
by ikegami (Patriarch) on Dec 31, 2005 at 21:25 UTC |