結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー tomeruntomerun
提出日時 2022-05-20 22:04:21
言語 Crystal
(1.11.2)
結果
AC  
実行時間 122 ms / 3,000 ms
コード長 1,984 bytes
コンパイル時間 15,268 ms
コンパイル使用メモリ 289,820 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2023-10-20 12:45:02
合計ジャッジ時間 17,490 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 72 ms
10,084 KB
testcase_08 AC 30 ms
10,344 KB
testcase_09 AC 83 ms
10,344 KB
testcase_10 AC 78 ms
10,344 KB
testcase_11 AC 85 ms
10,368 KB
testcase_12 AC 82 ms
10,104 KB
testcase_13 AC 71 ms
10,272 KB
testcase_14 AC 68 ms
10,116 KB
testcase_15 AC 68 ms
10,116 KB
testcase_16 AC 21 ms
10,320 KB
testcase_17 AC 122 ms
10,196 KB
testcase_18 AC 20 ms
10,196 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 92 ms
10,220 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 43 ms
10,304 KB
testcase_25 AC 79 ms
10,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

DR = [1, 0, -1, 0]
DC = [0, 1, 0, -1]
h, w, y, x = read_line.split.map(&.to_i)
y -= 1
x -= 1
a = Array.new(h) { read_line.split.map(&.to_i64) }
cur = a[y][x]
visited = Array.new(h) { Array.new(w, false) }
visited[y][x] = true
q = PriorityQueue({Int64, Int32, Int32}).new(h * w)
4.times do |i|
  ny = y + DR[i]
  nx = x + DC[i]
  if 0 <= ny && ny < h && 0 <= nx && nx < w
    visited[ny][nx] = true
    q.add({-a[ny][nx], ny, nx})
  end
end
while q.size > 0
  v, cy, cx = q.pop
  v *= -1
  if v >= cur
    puts "No"
    exit
  end
  cur += v
  4.times do |i|
    ny = cy + DR[i]
    nx = cx + DC[i]
    if 0 <= ny && ny < h && 0 <= nx && nx < w && !visited[ny][nx]
      visited[ny][nx] = true
      q.add({-a[ny][nx], ny, nx})
    end
  end
end
puts "Yes"

class PriorityQueue(T)
  def initialize(capacity : Int32)
    @elem = Array(T).new(capacity)
  end

  def initialize(list : Enumerable(T))
    @elem = list.to_a
    1.upto(size - 1) { |i| fixup(i) }
  end

  def size
    @elem.size
  end

  def add(v)
    @elem << v
    fixup(size - 1)
  end

  def top
    @elem[0]
  end

  def pop
    ret = @elem[0]
    last = @elem.pop
    if size > 0
      @elem[0] = last
      fixdown(0)
    end
    ret
  end

  def clear
    @elem.clear
  end

  def decrease_top(new_value : T)
    @elem[0] = new_value
    fixdown(0)
  end

  def to_s(io : IO)
    io << @elem
  end

  private def fixup(index : Int32)
    while index > 0
      parent = (index - 1) // 2
      break if @elem[parent] >= @elem[index]
      @elem[parent], @elem[index] = @elem[index], @elem[parent]
      index = parent
    end
  end

  private def fixdown(index : Int32)
    while true
      left = index * 2 + 1
      break if left >= size
      right = index * 2 + 2
      child = right >= size || @elem[left] > @elem[right] ? left : right
      if @elem[child] > @elem[index]
        @elem[child], @elem[index] = @elem[index], @elem[child]
        index = child
      else
        break
      end
    end
  end
end
0