結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー gew1fw
提出日時 2025-06-12 13:15:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,732 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 82,368 KB
実行使用メモリ 114,188 KB
最終ジャッジ日時 2025-06-12 13:17:26
合計ジャッジ時間 8,954 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    H = int(data[idx])
    idx += 1
    W = int(data[idx])
    idx += 1
    Y = int(data[idx])
    idx += 1
    X = int(data[idx])
    idx += 1
    
    A = []
    for _ in range(H):
        row = list(map(int, data[idx:idx+W]))
        A.append(row)
        idx += W
    
    accessible = [[False] * (W + 2) for _ in range(H + 2)]
    added = [[False] * (W + 2) for _ in range(H + 2)]
    
    current_attack = A[Y-1][X-1]
    accessible[Y][X] = True
    
    heap = []
    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    
    for dy, dx in directions:
        ny = Y + dy
        nx = X + dx
        if 1 <= ny <= H and 1 <= nx <= W:
            if (ny, nx) == (Y, X):
                continue
            if A[ny-1][nx-1] <= current_attack and not added[ny][nx]:
                heapq.heappush(heap, (A[ny-1][nx-1], ny, nx))
                added[ny][nx] = True
    
    defeated_count = 0
    total_enemies = H * W - 1
    
    while heap:
        a, i, j = heapq.heappop(heap)
        if accessible[i][j]:
            continue
        accessible[i][j] = True
        defeated_count += 1
        current_attack += a
        
        for dy, dx in directions:
            ni = i + dy
            nj = j + dx
            if 1 <= ni <= H and 1 <= nj <= W:
                if not accessible[ni][nj] and (ni, nj) != (Y, X):
                    if A[ni-1][nj-1] <= current_attack and not added[ni][nj]:
                        heapq.heappush(heap, (A[ni-1][nj-1], ni, nj))
                        added[ni][nj] = True
    
    print("Yes" if defeated_count == total_enemies else "No")

if __name__ == "__main__":
    main()
0