解题思路:
注意事项:
参考代码:
from collections import deque
n=int(input())
m=[[i for i in input()]for _ in range(n)]
v=[[0 for _ in range(n)] for _ in range(n)]
dir=[[1,0],[-1,0],[0,1],[0,-1]]
flag=True
def bfs(x,y):
q=deque([(x,y)])
global flag
while q:
x,y=q.popleft()
if m[x+1][y]=='#' and m[x-1][y]=='#' and m[x][y+1]=='#' and m[x][y-1]=='#':
flag=False
for i in range(4):
nx=x+dir[i][0]
ny=y+dir[i][1]
if 0<=nx<n and 0<=ny<n and m[nx][ny]=="#" and v[nx][ny]==0:
v[nx][ny]=1
q.append((nx,ny))
ans=0
for i in range(n):
for j in range(n):
if m[i][j]=='#'and v[i][j]==0:
v[i][j]=1
flag=True
bfs(i,j)
if flag==True:
ans+=1
print(ans)
0.0分
0 人评分