先上代码
from collections import deque
N=2010
g,q=[[]],deque()
ans=[[0]*N for i in range(N)]
for i in range(1,11):
g.append('0'+input())
# print(g[1])
n,m=10,10
def dfs(x,y):
if d[x][y]:
while q:
tmp=q.popleft()
xx,yy=tmp[0],tmp[1]
ans[xx][yy]=-1
return
if x==0 or x==n+1 or y==0 or y==m+1:
while q:
tmp=q.popleft()
xx,yy=tmp[0],tmp[1]
ans[xx][yy]=1
return
d[x][y]=1
q.append((x,y))
if g[x][y]=='U':
dfs(x-1,y)
if g[x][y]=='D':
dfs(x+1,y)
if g[x][y]=='L':
dfs(x,y-1)
if g[x][y]=='R':
dfs(x,y+1)
for i in range(1,11):
for j in range(1,11):
if ans[i][j]!=0:
continue
d=[[0]*N for i in range(N)]
dfs(i,j)
cnt=0
for i in range(1,11):
for j in range(1,11):
if ans[i][j]==1:
cnt+=1
print(cnt)