Submission #515014


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..1
    for c in 0..2
      if map[r][c] == map[r+1][c]
        chokudai += ROW_SCORE[r][c]
      end
    end
  end

  for r in 0..2
    for c in 0..1
      if map[r][c] == map[r][c+1]
        chokudai += COL_SCORE[r][c]
      end
    end
  end
  chokudai
end

def game(map, depth)
  return SUM - compute_score(map) if depth == 10

  return MEMO[map] unless MEMO[map].nil?

  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, 1)
naoko = [ROW_SCORE, COL_SCORE].flatten.inject(:+) - chokudai

puts chokudai
puts naoko

Submission Info

Submission Time
Task C - 双子と○×ゲーム
User hamadu
Language Ruby (2.1.5p273)
Score 100
Code Size 1111 Byte
Status AC
Exec Time 273 ms
Memory 5352 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 5352 KB
sample-02.txt AC 267 ms 5148 KB
test-01.txt AC 269 ms 5224 KB
test-02.txt AC 268 ms 5220 KB
test-03.txt AC 268 ms 5324 KB
test-04.txt AC 269 ms 5224 KB
test-05.txt AC 265 ms 5216 KB
test-06.txt AC 267 ms 5152 KB
test-07.txt AC 267 ms 5272 KB
test-08.txt AC 272 ms 5228 KB
test-09.txt AC 267 ms 5228 KB
test-10.txt AC 269 ms 5224 KB
test-11.txt AC 266 ms 5224 KB
test-12.txt AC 263 ms 5228 KB
test-13.txt AC 268 ms 5228 KB
test-14.txt AC 265 ms 5228 KB
test-15.txt AC 269 ms 5228 KB
test-16.txt AC 266 ms 5228 KB
test-17.txt AC 270 ms 5224 KB
test-18.txt AC 267 ms 5228 KB
test-19.txt AC 267 ms 5224 KB
test-20.txt AC 273 ms 5228 KB