https://programmers.co.kr/learn/courses/30/lessons/60061
def solution(n, build_frame):
answer = []
bos=[]
pils =[]
def ck_bo(x,y, add=True):
if (((x,y-1) in pils) or ((x+1,y-1) in pils)) or (((x-1, y) in bos) and ((x+1, y) in bos)):
if add:
bos.append((x,y))
return True
return False
def ck_pil(x,y, add=True):
if ((x, y-1) in pils) or ((x,y) in bos) or ((x-1, y) in bos) or y ==0:
if add:
pils.append((x,y))
return True
return False
def del_bo(x,y):
try:
bos.remove((x,y))
if (x-1,y) in bos:
if not ck_bo(x-1, y, False):
bos.append((x,y))
return False
if (x+1,y) in bos:
if not ck_bo(x+1, y, False):
bos.append((x,y))
return False
if (x,y) in pils:
if not ck_pil(x,y, False):
bos.append((x,y))
return False
if (x+1, y) in pils:
if not ck_pil(x+1, y,False):
bos.append((x,y))
return False
return True
except:
return False
def del_pil(x,y):
try:
pils.remove((x,y))
if (x,y+1) in bos:
if not ck_bo(x, y+1, False):
pils.append((x,y))
return False
if (x-1,y+1) in bos:
if not ck_bo(x-1, y+1, False):
pils.append((x,y))
return False
if (x,y+1) in pils:
if not ck_pil(x,y+1, False):
pils.append((x,y))
return False
return True
except:
return False
for frame in build_frame:
x, y = frame[0], frame[1]
if frame[-1] == 1:
if frame[-2] == 0:
ck_pil(x,y)
else:
ck_bo(x,y)
else:
if frame[-2] == 0:
del_pil(x,y)
else:
del_bo(x,y)
bos = [list(bo)+[1] for bo in bos]
pils = [list(pil)+[0] for pil in pils]
answer = bos + pils
answer.sort()
return answer