https://zoj.pintia.cn/problem-sets/91827364500/problems/91827365208
第一次用Python来A算法题
习惯问题 和用C写的大差不差的样子。。
import numpy as np
import queue
def bfs(e,book,n,m,x,y):
next=[[0,-1],[-1,0],[0,1],[1,0],[-1,-1],[-1,1],[1,1],[1,-1]]
que=queue.Queue()
tx,ty=x,y
tmp=(tx,ty)
que.put(tmp)
book[tx][ty]=1
while que.empty()==False:
cur=que.get()
for i in range(8):
tx,ty=cur[0]+next[i][0],cur[1]+next[i][1]
if 0<=tx and tx<n and 0<=ty and ty<m and e[tx][ty]=='@' and book[tx][ty]==0:
que.put((tx,ty))
book[tx][ty]=1
def solve(n,m,e):
book=np.zeros((n,m))
ans=int(0)
for i in range(n):
for j in range(m):
if e[i][j]=='@' and book[i][j]==0:
bfs(e,book,n,m,i,j)
ans=ans+1
return ans
while 1:
n,m=map(int,input().split())
if n==0:
break;
e=[]
for i in range(n):
str=input()
tmp=[]
for j in range(m):
tmp.append(str[j])
e.append(tmp)
print(solve(n,m,e))
"""
3 5
*@*@*
**@**
*@*@*
m=input()
for i in m.split():
print(i)
"""