1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import sys
sys.setrecursionlimit(600000)
def dfs(i, j): global flag, vis, mp, Dir if mp[i + 1][j] == "#" and mp[i - 1][j] == "#" and \ mp[i][j + 1] == "#" and mp[i][j - 1] == "#": flag = True for di, dj in Dir: ni, nj = i + di, j + dj if not vis[ni][nj] and mp[ni][nj] == "#": vis[ni][nj] = True dfs(ni, nj)
ans = 0 n = int(input()) mp = [] vis = [[False] * n for _ in range(n)] Dir = [(1, 0), (-1, 0), (0, 1), (0, -1)] for _ in range(n): mp.append(list(input())) for x in range(n): for y in range(n): if mp[x][y] == "." or vis[x][y]: continue flag = False vis[x][y] = True dfs(x, y) if not flag: ans += 1 print(ans)
|