Submission #74294027


Source Code Expand

from collections import deque

h, w = map(int, input().split())
grid = []

for _ in range(h):
    grid.append(list(input()))

visited = [[False] * w for _ in range(h)]
ans = 0

for i in range(h):
    for j in range(w):
        if grid[i][j] == "." and not visited[i][j]:
            queue = deque([(i, j)])
            visited[i][j] = True
            is_surrounded = True
            
            while queue:
                r, c = queue.popleft()
                if r == 0 or r == h-1 or c == 0 or c == w-1:
                    is_surrounded = False
                
                for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                    nr, nc = r + dr, c + dc
                    if 0 <= nr < h and 0 <= nc < w:
                        if grid[nr][nc] == "." and not visited[nr][nc]:
                            visited[nr][nc] = True
                            queue.append((nr, nc))
            
            if is_surrounded:
                ans += 1

print(ans)

Submission Info

Submission Time
Task C - Puddles
User gett
Language Python (PyPy 3.11-v7.3.20)
Score 300
Code Size 1018 Byte
Status AC
Exec Time 272 ms
Memory 160136 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 2
AC × 24
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All min.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
min.txt AC 66 ms 93824 KiB
random_01.txt AC 169 ms 157032 KiB
random_02.txt AC 107 ms 123076 KiB
random_03.txt AC 166 ms 156828 KiB
random_04.txt AC 128 ms 132540 KiB
random_05.txt AC 205 ms 157440 KiB
random_06.txt AC 90 ms 110144 KiB
random_07.txt AC 203 ms 157616 KiB
random_08.txt AC 96 ms 110868 KiB
random_09.txt AC 268 ms 158620 KiB
random_10.txt AC 221 ms 146732 KiB
random_11.txt AC 272 ms 158960 KiB
random_12.txt AC 91 ms 109096 KiB
random_13.txt AC 250 ms 160036 KiB
random_14.txt AC 97 ms 112220 KiB
random_15.txt AC 252 ms 160136 KiB
random_16.txt AC 138 ms 124368 KiB
random_17.txt AC 116 ms 152536 KiB
random_18.txt AC 221 ms 159548 KiB
random_19.txt AC 182 ms 157120 KiB
random_20.txt AC 148 ms 156608 KiB
random_21.txt AC 148 ms 156576 KiB
sample_01.txt AC 66 ms 93772 KiB
sample_02.txt AC 66 ms 93936 KiB