結果

問題 No.34 砂漠の行商人
ユーザー letrangerjpletrangerjp
提出日時 2017-06-13 21:40:28
言語 Ruby
(3.3.0)
結果
AC  
実行時間 2,211 ms / 5,000 ms
コード長 976 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 11,504 KB
実行使用メモリ 19,248 KB
最終ジャッジ日時 2023-09-10 19:28:18
合計ジャッジ時間 9,156 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
15,260 KB
testcase_01 AC 85 ms
15,056 KB
testcase_02 AC 92 ms
15,544 KB
testcase_03 AC 85 ms
15,072 KB
testcase_04 AC 180 ms
15,652 KB
testcase_05 AC 297 ms
15,856 KB
testcase_06 AC 112 ms
15,508 KB
testcase_07 AC 478 ms
15,648 KB
testcase_08 AC 707 ms
16,024 KB
testcase_09 AC 90 ms
15,288 KB
testcase_10 AC 89 ms
15,312 KB
testcase_11 AC 87 ms
15,400 KB
testcase_12 AC 131 ms
15,816 KB
testcase_13 AC 91 ms
15,408 KB
testcase_14 AC 88 ms
15,304 KB
testcase_15 AC 86 ms
15,440 KB
testcase_16 AC 88 ms
15,504 KB
testcase_17 AC 88 ms
15,208 KB
testcase_18 AC 84 ms
15,256 KB
testcase_19 AC 1,359 ms
19,248 KB
testcase_20 AC 2,211 ms
19,112 KB
testcase_21 AC 95 ms
15,496 KB
testcase_22 AC 88 ms
15,308 KB
testcase_23 AC 91 ms
15,448 KB
testcase_24 AC 90 ms
15,400 KB
testcase_25 AC 253 ms
15,828 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, V, SX, SY, GX, GY = gets.split.take(6).map(&:to_i)
MAP = N.times.map{ gets.split.take(N).map(&:to_i) }
MAX_DMG = MAP.map{|y|y.max}.max

def f(start, last)
  # マンハッタン距離 * 最大ダメージ < hp の枝刈り
  ml = (last[0] - start[0]).abs + (last[1] - start[1]).abs
  return ml if V > ml * MAX_DMG

  visited = N.times.map{[-1]*N}
  visited[start[0]][start[1]] = V
  q = [[0, V] + start]

  res = -1
  while !q.empty?
    step, hp, x, y = q.shift
    if [x, y] == last
      res = step
      break
    else
    end

    [[x+1, y], [x-1, y], [x, y+1], [x, y-1]].each{|a, b|
      next if !a.between?(0, N-1)
      next if !b.between?(0, N-1)
      _hp = hp - MAP[b][a]
      next if _hp < 1
      _step = step + 1
      if visited[a][b] < _hp
        visited[a][b] = _hp
        elem = [_step, _hp, a, b]
        # q[q.bsearch_index{|v|(v <=> elem) < 0}||q.size, 0] = [elem]
        q << elem
      end
    }
  end
  res
end

p f([SX-1, SY-1], [GX-1, GY-1])
0