結果

問題 No.86 TVザッピング(2)
ユーザー gew1fw
提出日時 2025-06-12 17:59:29
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,181 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 69,096 KB
最終ジャッジ日時 2025-06-20 14:02:43
合計ジャッジ時間 2,462 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
grid = [input().strip() for _ in range(n)]

cells = []
for i in range(n):
    for j in range(m):
        if grid[i][j] == '.':
            cells.append((i, j))

k = len(cells)
if k % 2 != 0:
    print("NO")
    exit()

# Check if all cells are in the same row or column
same_row = True
row = cells[0][0]
for i, j in cells:
    if i != row:
        same_row = False
        break

same_col = True
col = cells[0][1]
for i, j in cells:
    if j != col:
        same_col = False
        break

if same_row or same_col:
    print("NO")
    exit()

# Check connectivity using BFS
from collections import deque

visited = [[False] * m for _ in range(n)]
start_i, start_j = cells[0]
queue = deque([(start_i, start_j)])
visited[start_i][start_j] = True
count = 1

directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

while queue:
    i, j = queue.popleft()
    for di, dj in directions:
        ni, nj = i + di, j + dj
        if 0 <= ni < n and 0 <= nj < m:
            if grid[ni][nj] == '.' and not visited[ni][nj]:
                visited[ni][nj] = True
                count += 1
                queue.append((ni, nj))

print("YES" if count == k else "NO")
0