LoginSignup
0
0

More than 5 years have passed since last update.

とあるカードゲーム

Last updated at Posted at 2018-10-06
  • 状態管理がなかなか難しいですね?
  • HTML構造がやや特殊なため、採点プログラムの改修が必要でした。

解法

  • 枚数が固定されているわけではないので、最初にカインドを全部処理してからストーリーを全部処理するみたいな解法は使えません。
  • 計算量を 2**N (N==8) から減らすのはなかなか難しいのではないでしょうか。

.()記法

答案

(多分Ruby 1.9以降で動くはず)

tyama_henae27.rb
#!/usr/bin/ruby
#http://nabetani.sakura.ne.jp/hena/orde27cardgame/
#https://qiita.com/Nabetani/items/cdc102d186faaf542574

def dfs(a,msk)
    $memo[msk]||=->{
        idx_=(0...a.size).find{|i|msk[i]==0}
        if !idx_
            0
        else
            r=nil
            2.times{|k|
                idx=idx_
                add=1<< idx
                c=1
                while idx=(idx+1...a.size).find{|i|o=a[idx_].dup;o[k]+=c;a[i]==o&&msk[i]==0}
                    add|=1<< idx
                    r0=dfs(a,msk|add)
                    if r0
                        r=[r.to_i,c**2+r0].max
                    end
                    c+=1
                end
            }
            r
        end 
    }.()
end

while gets
    $memo={}
    a=$_.chomp.split(',').map{|e|[e[0].ord-65,e[1].ord-49]}.sort
    puts dfs(a,0)||'-'
    STDOUT.flush
end
  • どうでもいいけど、1<<idxでシンタックスハイライトが壊れるのなんとかしてくれ
0
0
0

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
0
0