From: James Edward Gray II Date: 2007-10-08T02:28:36+09:00 Subject: Re: [QUIZ] Itinerary for a Traveling Salesman (#142) On Oct 7, 2007, at 9:20 AM, James Koppel wrote: > First, before I get to my main solution, I would like to show that, > as Morton pointed out, yes, it is very possible to have a quick, > exact solution. Here's one that does so for N >= 3 (N=2 and N=1 > nevertheless draw correctly; a call to uniq would make them work). > > require 'RMagick' > require 'enumerator' > > > > n = ARGV[0].to_i > > itin = ([0]*n).zip((0...n).to_a) > > 1.step(n-3, 2) do |x| > itin += ([x]*(n-1)).zip((1...n).to_a.reverse) > itin += ([x+1]*(n-1)).zip((1...n).to_a) > end > > if n%2==0 > itin += ([n-1]*(n-1)).zip((0...n).to_a.reverse) > else > (n-1)..step(2, -2) do |y| > itin += [[n-2,y],[n-1,y],[n-1,y-1],[n-2,y-1]] > end > end > > itin += (0...n).to_a.reverse.zip([0]*n) > > $grid = Grid.new(n) #Because draw_itin asks for it > draw_itin(itin, "#{n}x#{n}tour2.jpg") I believe there's a typo in the above. Shouldn't it be: (n-1).step(2, -2) do |y| # I removed a . ? James Edward Gray II