結果

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

ソースコード

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]) - 1; idx += 1  # convert to 0-based
    X = int(data[idx]) - 1; idx += 1
    
    grid = []
    for i in range(H):
        row = list(map(int, data[idx:idx+W]))
        grid.append(row)
        idx += W
    
    heap = []
    visited = [[False for _ in range(W)] for _ in range(H)]
    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    
    current_attack = grid[Y][X]
    visited[Y][X] = True
    
    # Push initial adjacent cells
    for dx, dy in directions:
        nx = Y + dx
        ny = X + dy
        if 0 <= nx < H and 0 <= ny < W:
            a = grid[nx][ny]
            heapq.heappush(heap, (a, nx, ny))
    
    count = 0
    total_enemies = H * W - 1
    
    while heap:
        a, i, j = heapq.heappop(heap)
        if visited[i][j]:
            continue
        if a > current_attack:
            continue
        
        visited[i][j] = True
        current_attack += a
        count += 1
        
        if count == total_enemies:
            break
        
        for dx, dy in directions:
            ni = i + dx
            nj = j + dy
            if 0 <= ni < H and 0 <= nj < W:
                if not visited[ni][nj]:
                    heapq.heappush(heap, (grid[ni][nj], ni, nj))
    
    if count == total_enemies:
        print("Yes")
    else:
        print("No")

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