LoginSignup
4
4

More than 5 years have passed since last update.

くねくね増加列

Last updated at Posted at 2014-07-06

携帯だけで書いた下書き:http://ideone.com/36z2ZE

メモ化再帰にもっていけたのは「長くなるように、増え続けるように」(140805追記)のおかげだと思っています。

hena23.rb
#!/usr/bin/env ruby
#http://qiita.com/Nabetani/items/7ba11167ea28c929fcf2
#http://nabetani.sakura.ne.jp/hena/ord23snakemoinc/
class Hena23
    def initialize(a)
        @a=a
        @memo={}
    end
    def dfs(x,y,nprev)
        return 0 if x<0||@a[0].size<=x || y<0||@a.size<=y || @a[y][x]<=nprev
        return @memo[[x,y]]||=[[-1,0],[1,0],[0,-1],[0,1]].map{|_x,_y|
            dfs(x+_x,y+_y,@a[y][x])+1
        }.max
    end
    def run
        @a.size.times.lazy.map{|y|
            @a[0].size.times.lazy.map{|x|
                dfs(x,y,-1)
            }.max
        }.max
    end
end

if $0==__FILE__
    STDOUT.sync=true
    while gets
        p Hena23.new($_.chomp.split('/').map{|e|e.split('').map(&:to_i)}).run
    end
end
4
4
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4