結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー roaris
提出日時 2022-05-20 23:20:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 395 ms / 3,000 ms
コード長 798 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 82,136 KB
実行使用メモリ 91,440 KB
最終ジャッジ日時 2024-09-20 09:43:38
合計ジャッジ時間 4,938 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *
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)]
f = [[False]*W for _ in range(H)]
f[X][Y] = True
pq = []

for nx, ny in [(X-1, Y), (X+1, Y), (X, Y-1), (X, Y+1)]:
    if 0<=nx<H and 0<=ny<W:
        heappush(pq, (A[nx][ny], nx*1000+ny))
        f[nx][ny] = True

now = A[X][Y]

while pq:
    if pq[0][0]<now:
        a, xy = heappop(pq)
        x, y = divmod(xy, 1000)
        now += a
        
        for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
            if 0<=nx<H and 0<=ny<W and not f[nx][ny]:
                heappush(pq, (A[nx][ny], nx*1000+ny))
                f[nx][ny] = True
    else:
        exit(print('No'))
    
print('Yes')
0