結果

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

ソースコード

diff #

import heapq

def main():
    H, W, Y, X = map(int, input().split())
    Y -= 1  # Convert to 0-based index
    X -= 1
    grid = []
    for _ in range(H):
        grid.append(list(map(int, input().split())))
    
    current_power = grid[Y][X]
    visited = [[False for _ in range(W)] for _ in range(H)]
    heap = []
    directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    
    # Initialize the heap with adjacent cells of the starting position
    for dy, dx in directions:
        ny = Y + dy
        nx = X + dx
        if 0 <= ny < H and 0 <= nx < W:
            if not visited[ny][nx]:
                heapq.heappush(heap, (grid[ny][nx], ny, nx))
                visited[ny][nx] = True
    
    total_enemies = H * W - 1  # Exclude the starting position
    count = 0
    
    while heap:
        power, i, j = heapq.heappop(heap)
        if current_power <= power:
            print("No")
            return
        current_power += power
        count += 1
        if count == total_enemies:
            print("Yes")
            return
        # Add adjacent cells to the heap
        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 (ny != Y or nx != X):
                    heapq.heappush(heap, (grid[ny][nx], ny, nx))
                    visited[ny][nx] = True
    
    # If heap is empty but not all enemies are processed
    print("No")

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