Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Boundary Blocks by eugene100372
from typing import List, Tuple, Iterable
def neighbors(i,j):
return set((i+di,j+dj) for di,dj in ((0,1),(0,-1),(1,0),(-1,0)))
def boundary_blocks(grid: List[str]) -> Iterable[Tuple[int]]:
s1,s2=set(),set()
for i,st in enumerate(grid):
for j,sb in enumerate(st):
if sb=='.': s1.add((i,j))
else: s2.add((i,j))
areas=[]
while s1:
new={s1.pop()}
area=set(new)
while new:
newest=set()
for ceil in new: newest|=(neighbors(*ceil)&s1)
new=newest-new
s1-=new
area|=new
areas.append(area)
res=[]
for ceil in s2:
if sum(bool(neighbors(*ceil)&area) for area in areas)>1: res.append(ceil)
return res
Sept. 24, 2020
Comments: