結果

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

ソースコード

diff #

import sys
import heapq
from collections import deque

def main():
    H, W, Y, X = map(int, sys.stdin.readline().split())
    Y -= 1
    X -= 1
    grid = []
    for _ in range(H):
        row = list(map(int, sys.stdin.readline().split()))
        grid.append(row)
    
    processed = [[False for _ in range(W)] for _ in range(H)]
    in_queue = [[False for _ in range(W)] for _ in range(H)]
    
    bfs_queue = deque()
    bfs_queue.append((Y, X))
    processed[Y][X] = True
    
    heap = []
    S = grid[Y][X]
    
    # Initial BFS to find all reachable enemies with attack <= S
    while bfs_queue:
        i, j = bfs_queue.popleft()
        for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            ni = i + di
            nj = j + dj
            if 0 <= ni < H and 0 <= nj < W:
                if not processed[ni][nj]:
                    if (ni, nj) != (Y, X) and grid[ni][nj] <= S:
                        if not in_queue[ni][nj]:
                            heapq.heappush(heap, (grid[ni][nj], ni, nj))
                            in_queue[ni][nj] = True
    
    while heap:
        ai, i, j = heapq.heappop(heap)
        if ai > S:
            print("No")
            return
        if processed[i][j]:
            continue
        processed[i][j] = True
        S += ai
        
        # Add this cell to BFS queue to explore further
        bfs_queue.append((i, j))
        
        # Explore neighbors to find new reachable cells
        while bfs_queue:
            ci, cj = bfs_queue.popleft()
            for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                ni = ci + di
                nj = cj + dj
                if 0 <= ni < H and 0 <= nj < W:
                    if not processed[ni][nj]:
                        if (ni, nj) != (Y, X) and grid[ni][nj] <= S:
                            if not in_queue[ni][nj]:
                                heapq.heappush(heap, (grid[ni][nj], ni, nj))
                                in_queue[ni][nj] = True
    
    # Check if all enemies are processed
    total_enemies = 0
    processed_enemies = 0
    for i in range(H):
        for j in range(W):
            if (i, j) != (Y, X):
                total_enemies += 1
                if processed[i][j]:
                    processed_enemies += 1
    if processed_enemies == total_enemies:
        print("Yes")
    else:
        print("No")

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