結果
問題 |
No.1949 足し算するだけのパズルゲーム(2)
|
ユーザー |
![]() |
提出日時 | 2025-06-12 13:18:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,021 ms / 3,000 ms |
コード長 | 1,501 bytes |
コンパイル時間 | 366 ms |
コンパイル使用メモリ | 82,748 KB |
実行使用メモリ | 100,788 KB |
最終ジャッジ日時 | 2025-06-12 13:21:17 |
合計ジャッジ時間 | 9,519 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
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()