結果

問題 No.86 TVザッピング(2)
ユーザー lam6er
提出日時 2025-03-20 21:11:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,193 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 70,624 KB
最終ジャッジ日時 2025-06-20 14:01:48
合計ジャッジ時間 2,365 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    sys.setrecursionlimit(1 << 25)
    N, M = map(int, sys.stdin.readline().split())
    grid = [sys.stdin.readline().strip() for _ in range(N)]
    
    # Collect all viewable cells
    cells = []
    for i in range(N):
        for j in range(M):
            if grid[i][j] == '.':
                cells.append((i, j))
    K = len(cells)
    if K == 0:
        print("NO")
        return
    
    # Check if all cells are connected
    from collections import deque
    visited = [[False]*M for _ in range(N)]
    start = cells[0]
    q = deque([start])
    visited[start[0]][start[1]] = True
    connected = 0
    directions = [(-1,0),(1,0),(0,-1),(0,1)]
    while q:
        i, j = q.popleft()
        connected += 1
        for di, dj in directions:
            ni, nj = i+di, j+dj
            if 0<=ni<N and 0<=nj<M and grid[ni][nj] == '.' and not visited[ni][nj]:
                visited[ni][nj] = True
                q.append((ni, nj))
    if connected != K:
        print("NO")
        return
    
    # Check if the number of viewable cells is even
    if K % 2 == 0:
        print("YES")
    else:
        print("NO")

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