From: Robert Klemme Date: 2008-05-09T21:57:23+09:00 Subject: Re: Cookie Monster! 2008/5/9 Kai Krakow : >> > Seems like a comp sci homework problem to me, as it is a classic example >> > of where you can use dynamic programming. Seehttp://en.wikipedia.org/wiki/Dynamic_programming >> >> Actually It's not homework. You don't normally get homework at my age. >> Just something I thought would be interesting.. >> >> > I think it's a perfect quiz for Ruby Quiz. >> >> Ah ok, shows how wrong I was.. I'll submit something to Ruby Quiz. >> Thanks. > > Shouldn't this easily and optimally solvable with Dijkstra's > algorithm: > http://en.wikipedia.org/wiki/Dijkstra's_algorithm > > Dijkstra solves the problem bottom-up, solving smaller problems first > and then solve the next bigger problem. One just has to make sure to > negate the optimizer condition and to walk north and west (because > Dijkstra's algorithm returns the previous nodes of the path, not the > next nodes). This should be in sense of "dynamic programming" as > mentioned before. I did a rather straightforward backtracking implementation: 14:54:06 OZ-27759$ time /c/Temp/cookie.rb # real 0m0.495s user 0m0.436s sys 0m0.061s 14:54:33 OZ-27759$ wc /c/Temp/cookie.rb 76 295 1617 /c/Temp/cookie.rb 14:55:06 OZ-27759$ Kind regards robert -- use.inject do |as, often| as.you_can - without end