結果

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

ソースコード

diff #

import heapq

def main():
    H, W, Y, X = map(int, input().split())
    Y -= 1  # convert to 0-based
    X -= 1
    grid = []
    for _ in range(H):
        grid.append(list(map(int, input().split())))
    
    visited = [[False]*W for _ in range(H)]
    visited[Y][X] = True
    current_power = grid[Y][X]
    heap = []
    directions = [ (-1,0), (1,0), (0,-1), (0,1) ]
    
    # Initialize the heap with adjacent cells
    for dy, dx in directions:
        ny = Y + dy
        nx = X + dx
        if 0 <= ny < H and 0 <= nx < W:
            if not visited[ny][nx] and grid[ny][nx] <= current_power:
                heapq.heappush(heap, (grid[ny][nx], ny, nx))
    
    required = H * W - 1  # since starting cell is not counted
    count = 0
    
    while heap:
        attack, i, j = heapq.heappop(heap)
        if visited[i][j]:
            continue
        if current_power < attack:
            print("No")
            return
        current_power += attack
        visited[i][j] = True
        count += 1
        # Check adjacent cells
        for dy, dx in directions:
            ny = i + dy
            nx = j + dx
            if 0 <= ny < H and 0 <= nx < W:
                if not visited[ny][nx] and grid[ny][nx] <= current_power:
                    heapq.heappush(heap, (grid[ny][nx], ny, nx))
    
    if count == required:
        print("Yes")
    else:
        print("No")

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