AcWing 1233. 全球变暖
原题链接
简单
作者:
Kuro.
,
2024-01-13 22:29:49
,
所有人可见
,
阅读 44
from collections import deque
N = 1010
g=[['' for _ in range(N)] for _ in range(N)]
st=[[False for _ in range(N)] for _ in range(N)]
def bfs(x,y):
global res
q=deque()
q.append([x,y])
st[x][y]=True
dx=[0,1,0,-1]
dy=[1,0,-1,0]
total,bound=0,0
while q:
is_bound=False
t=q.popleft()
total+=1
for i in range(4):
a,b=t[0]+dx[i],t[1]+dy[i]
if a<1 or b<1 or a>n or b>n:
continue
if g[a][b]=='.':
is_bound =True
continue
if st[a][b]:
continue
st[a][b]=True
q.append([a,b])
if is_bound:
bound+=1
#print(total,bound)
if total == bound:
res+=1
n=int(input())
res=0
for i in range(1,n+1):
g[i][1:n+1]=input()
for i in range(1,n+1):
for j in range(1,n+1):
if g[i][j]=='#' and not st[i][j]:
bfs(i,j)
print(res)