Submission #515017


Source Code Expand

ROW_SCORE = (1..2).map do
  gets.split(' ').map(&:to_i)
end
COL_SCORE = (1..3).map do
  gets.split(' ').map(&:to_i)
end
SUM = [ROW_SCORE, COL_SCORE].flatten.inject(:+)
MEMO = {}

def compute_score(map)
  chokudai = 0
  for r in 0..2
    for c in 0..2
      chokudai += ROW_SCORE[r][c] if r <= 1 && map[r][c] == map[r+1][c]
      chokudai += COL_SCORE[r][c] if c <= 1 && map[r][c] == map[r][c+1]
    end
  end
  chokudai
end

def game(map, depth)
  return MEMO[map] unless MEMO[map].nil?
  if depth == 9
    MEMO[map] = SUM - compute_score(map)
    return MEMO[map]
  end

  best = 0
  mark = (depth % 2) * 2 - 1

  for r in 0..2
    for c in 0..2
      if map[r][c] == 0
        map[r][c] = mark
        result = SUM - game(map, depth+1)
        if result > best
          best = result
        end
        map[r][c] = 0
      end
    end
  end

  MEMO[map] = best
  best
end

map = [[0,0,0],[0,0,0],[0,0,0]]
chokudai = game(map, 0)
naoko = SUM - chokudai

puts chokudai
puts naoko

Submission Info

Submission Time
Task C - 双子と○×ゲーム
User hamadu
Language Ruby (2.1.5p273)
Score 100
Code Size 1035 Byte
Status AC
Exec Time 292 ms
Memory 5516 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 22
Set Name Test Cases
Sample sample-01.txt, sample-02.txt
All test-01.txt, test-02.txt, test-03.txt, test-04.txt, test-05.txt, test-06.txt, test-07.txt, test-08.txt, test-09.txt, test-10.txt, test-11.txt, test-12.txt, test-13.txt, test-14.txt, test-15.txt, test-16.txt, test-17.txt, test-18.txt, test-19.txt, test-20.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
sample-01.txt AC 268 ms 5472 KB
sample-02.txt AC 273 ms 5472 KB
test-01.txt AC 272 ms 5468 KB
test-02.txt AC 292 ms 5440 KB
test-03.txt AC 273 ms 5508 KB
test-04.txt AC 274 ms 5472 KB
test-05.txt AC 272 ms 5472 KB
test-06.txt AC 272 ms 5472 KB
test-07.txt AC 270 ms 5504 KB
test-08.txt AC 269 ms 5472 KB
test-09.txt AC 270 ms 5472 KB
test-10.txt AC 274 ms 5472 KB
test-11.txt AC 270 ms 5472 KB
test-12.txt AC 270 ms 5476 KB
test-13.txt AC 269 ms 5472 KB
test-14.txt AC 269 ms 5472 KB
test-15.txt AC 272 ms 5472 KB
test-16.txt AC 270 ms 5476 KB
test-17.txt AC 270 ms 5516 KB
test-18.txt AC 270 ms 5476 KB
test-19.txt AC 270 ms 5476 KB
test-20.txt AC 269 ms 5472 KB