結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー 👑 rin204
提出日時 2022-05-20 22:02:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 904 ms / 3,000 ms
コード長 825 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 82,640 KB
実行使用メモリ 99,996 KB
最終ジャッジ日時 2024-09-20 08:10:04
合計ジャッジ時間 8,231 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *

h, w, x, y = map(int, input().split())
x -= 1
y -= 1
A = [list(map(int, input().split())) for _ in range(h)]
hq = []
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
tot = A[x][y]
used = [[False] * w for _ in range(h)]
used[x][y] = True

for dx, dy in directions:
    nx = x + dx
    ny = y + dy
    if nx == -1 or nx == h or ny == -1 or ny == w:
        continue
    heappush(hq, (A[nx][ny], nx, ny))
    used[nx][ny] = True

while hq:
    a, x, y = heappop(hq)
    if a >= tot:
        print("No")
        exit()
    tot += a
    for dx, dy in directions:
        nx = x + dx
        ny = y + dy
        if nx == -1 or nx == h or ny == -1 or ny == w:
            continue
        if used[nx][ny]:
            continue
        heappush(hq, (A[nx][ny], nx, ny))
        used[nx][ny] = True


print("Yes")
0