結果
| 問題 |
No.2946 Puyo
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-10-25 21:38:10 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 480 ms / 2,000 ms |
| コード長 | 1,280 bytes |
| コンパイル時間 | 208 ms |
| コンパイル使用メモリ | 82,460 KB |
| 実行使用メモリ | 126,088 KB |
| 最終ジャッジ日時 | 2024-10-25 21:38:27 |
| 合計ジャッジ時間 | 14,227 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
ソースコード
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.read
# 上下左右の移動方向
DIRECTIONS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def dfs(x, y, char, visited, component):
stack = [(x, y)]
while stack:
cx, cy = stack.pop()
if visited[cx][cy]:
continue
visited[cx][cy] = True
component.append((cx, cy))
for dx, dy in DIRECTIONS:
nx, ny = cx + dx, cy + dy
if 0 <= nx < H and 0 <= ny < W and not visited[nx][ny] and grid[nx][ny] == char:
stack.append((nx, ny))
# 入力の読み込み
data = input().splitlines()
H, W = map(int, data[0].split())
grid = [list(line) for line in data[1:]]
# 訪問済みのセルを管理するための配列
visited = [[False] * W for _ in range(H)]
# 連結成分を探して、条件に合うものを.に変換
for i in range(H):
for j in range(W):
if grid[i][j] != '.' and not visited[i][j]:
component = []
dfs(i, j, grid[i][j], visited, component)
# 連結成分のサイズが4以上ならば.に置き換え
if len(component) >= 4:
for x, y in component:
grid[x][y] = '.'
# 結果を出力
for row in grid:
print("".join(row))