結果

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

ソースコード

diff #

def main():
    import sys
    from collections import deque

    # Read input
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1

    grid = []
    for _ in range(N):
        grid.append(input[idx].strip())
        idx += 1

    # Count the number of '.' cells
    count = 0
    for row in grid:
        count += row.count('.')
    if count % 2 != 0:
        print("NO")
        return

    # Find the starting cell (any '.')
    start = None
    for i in range(N):
        for j in range(M):
            if grid[i][j] == '.':
                start = (i, j)
                break
        if start:
            break

    # If no start found (shouldn't happen as per problem statement)
    if not start:
        print("NO")
        return

    # BFS to check connectedness
    visited = [[False for _ in range(M)] for _ in range(N)]
    q = deque()
    q.append(start)
    visited[start[0]][start[1]] = True

    dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)]  # up, right, down, left

    while q:
        i, j = q.popleft()
        for di, dj in dirs:
            ni = i + di
            nj = j + dj
            if 0 <= ni < N and 0 <= nj < M:
                if grid[ni][nj] == '.' and not visited[ni][nj]:
                    visited[ni][nj] = True
                    q.append((ni, nj))

    # Check if all '.' are visited
    for i in range(N):
        for j in range(M):
            if grid[i][j] == '.' and not visited[i][j]:
                print("NO")
                return

    print("YES")

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