-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Here is my solution, which uses recursion like the Civilization II map generation example in Chris Pine's "Learn to Program" book. With a stack limit of: $ ulimit -s 8192 the maximum square size my solution can handle is 67x67. However, it can handle larger squares if you increase the stack size. Cheers. #!/usr/bin/ruby -w # @param 1 width of square # @param 2 starting row (X coordinate) # @param 3 starting column (Y coordinate) class Square < Array def initialize aWidth super(aWidth) { Array.new aWidth } @mark = 0 end # Walks this square, from the given position, # while marking unmarked (nil) cells. def walk x, y # skip invalid positions and marked cells return if x < 0 or x >= length or y < 0 or y >= length or self[x][y] # mark the current cell self[x][y] = @mark += 1 # walk to adjacent cells walk x + 3, y # east walk x + 2, y - 2 # north east walk x, y - 3 # north walk x - 2, y - 2 # north west walk x - 3, y # west walk x - 2, y + 2 # south west walk x, y + 3 # south walk x + 2, y + 2 # south east end # Pretty-prints this square. def to_s # pretty-print each row fmt = '|' << "%#{length.to_s.length * 2}d " * length << '|' lines = inject([]) do |memo, row| memo << fmt % row end # add a border to top & bottom border = '-' * lines.first.length lines.unshift border lines << border lines.join("\n") end end if $0 == __FILE__ # create a square with user's parameters width = (ARGV.shift || 5).to_i square = Square.new(width) # walk the square from random position origin = Array.new(2) { (ARGV.shift || rand(width)).to_i } square.walk(*origin) # pretty-print the walked square puts square.to_s end -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFE32aYmV9O7RYnKMcRAqRyAKCxvnyB1ncwLl4IhM6vg20wjKz9AACfTZgC yQFdSn2EBLE1YWrts0TGRXk= =4dbC -----END PGP SIGNATURE-----