結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー lloyz
提出日時 2022-05-21 01:04:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 921 ms / 3,000 ms
コード長 811 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 81,908 KB
実行使用メモリ 101,216 KB
最終ジャッジ日時 2024-09-20 10:54:35
合計ジャッジ時間 8,334 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

h, w, si, sj = map(int, input().split())
si -= 1
sj -= 1
A = [list(map(int, input().split())) for _ in range(h)]

visited = [[False for _ in range(w)] for _ in range(h)]
visited[si][sj] = True
Heap = []
Directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
curr = A[si][sj]
for di, dj in Directions:
    ni, nj = si + di, sj + dj
    if 0 <= ni < h and 0 <= nj < w:
        heappush(Heap, (A[ni][nj], ni, nj))
        visited[ni][nj] = True
while Heap:
    res, ci, cj = heappop(Heap)
    if curr <= res:
        print("No")
        exit()
    curr += res
    for di, dj in Directions:
        ni, nj = ci + di, cj + dj
        if 0 <= ni < h and 0 <= nj < w and not visited[ni][nj]:
            heappush(Heap, (A[ni][nj], ni, nj))
            visited[ni][nj] = True
print("Yes")
0