Snippets

cia_rana オフラインリアルタイムどう書く E08 の問題 - 白黒陣取りゲーム

Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
         x.each{|e|
           # x座標が一致しなければスキップ
           # x, y座標が一致しても自分の駒とエリアを構成する対角の駒との間に敵の駒があってもスキップ
-          if t == s && e == d && !enemies.index{|u, f| r <= u && u <= t && c <= f && f <= e }
+          if e == d && !enemies.index{|u, f| r <= u && u <= t && c <= f && f <= e }
             z = [t, e]
             flg = true
             break
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
 require "minitest/autorun"
 
-def scan_elements input
-  input.scan(/\d+/).map(&:to_i).zip(input.scan(/[a-z]/).map(&:ord))
-end
-
 def calc_area friends, enemies
   self_area = []
   friends.each{|r, c|
 end
 
 def solve input
-  w_input, b_input = input.split(?,)
-  w = scan_elements w_input
-  b = scan_elements b_input
+  w, b = input.split(?,).map{|e|e.scan(/\d+/).map(&:to_i).zip(e.scan(/[a-z]/).map(&:ord))}
   "%s,%s" % [calc_area(w, b), calc_area(b, w)]
 end
 
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
   }
   
   # 共通エリアの面積を引く
+  # 共通エリアが発生しうるのは右下の頂点を共有している場合のみ
   self_area.combination(2).each{|a, b|
     if a[2] == b[2] && a[3] == b[3]
       area_sum -= (a[2] - [a[0], b[0]].max) * (a[3] - [a[1], b[1]].max) 
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
         x.each{|e|
           # x座標が一致しなければスキップ
           # x, y座標が一致しても自分の駒とエリアを構成する対角の駒との間に敵の駒があってもスキップ
-          if t == s && e == d && !enemies.index{|s, d| r <= s && s <= t && c <= d && d <= e }
+          if t == s && e == d && !enemies.index{|u, f| r <= u && u <= t && c <= f && f <= e }
             z = [t, e]
             flg = true
             break
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
   "%s,%s" % [calc_area(w, b), calc_area(b, w)]
 end
 
-L=18
-def solve(is)
-  # 白黒の駒の座標のリスト
-  cl=is.split(?,).map{|s|s.scan(/([a-s])(\d+)/).map{|x,y|[x.ord-97,y.to_i-1]}}
-  #p cl
-  [0,1].map{|i|
-    # 駒の存在を示す2次元配列
-    ex=(0..L).map{[]}
-    # 陣地占領状況を示す2次元配列
-    oc=(1..L).map{[0]*L}
-    # 駒のx座標、y座標のリスト
-    xlist,ylist=[],[]
-    cl[i].each{|x,y|
-      xlist.push(x)
-      ylist.push(y)
-      ex[y][x]=1
-    }
-    # 逆の色の駒の標高マップ
-    elev=(0..L+1).map{[0]*(L+2)}
-    cl[1-i].each{|x,y|x.upto(L){|i|y.upto(L){|j|elev[j+1][i+1]+=1}}}
-
-    xlist=xlist.uniq.sort
-    ylist=ylist.uniq.sort
-    #p xlist,ylist,ex
-    xlist.combination(2).to_a.product(ylist.combination(2).to_a).each{|(x0,x1),(y0,y1)|
-      #p "#{x0},#{x1},#{y0},#{y1}"
-      # 4隅に駒がなければスキップ
-      next unless
-        ex[y0][x0]&&ex[y0][x1]&&ex[y1][x0]&&ex[y1][x1]
-      # 逆の色の駒を含んでいたらスキップ
-      # 4隅の標高から、含まれる逆の色の駒の個数を計算 ( しえるさんメソッド )
-      next if elev[y1+1][x1+1]-elev[y1+1][x0]-elev[y0][x1+1]+elev[y0][x0]>0
-      # 陣地を登録
-      (y0...y1).each{|y|
-        (x0...x1).each{|x|
-          oc[y][x]=1
-        }
-      }
-      #p "pass: #{x0},#{x1},#{y0},#{y1}"
-    }
-    # 最後に陣地をカウント
-    oc.reduce(0){|s,r|s+r.reduce(&:+)}
-  }*?,
-end
-
 class TestRunner < Minitest::Test
   def test_run
     assert_equal solve("d3d4e3e4d9h7h9j3j4j7j9,f4f6g4g5g6h5h6"), "5,3"
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.