https://school.programmers.co.kr/learn/courses/30/lessons/60061
def possible(result):
ROW = 1
COL = 0
for x, y, a in result:
if a == COL:
if y != 0 and (x, y - 1, COL) not in result and (x - 1, y, ROW) not in result and (x, y, ROW) not in result: \
return False
else:
if (x, y - 1, COL) not in result and (x + 1, y - 1, COL) not in result and not (
(x - 1, y, ROW) in result and (x + 1, y, ROW) in result):
return False
return True
def possible(result):
ROW = 1
COL = 0
for x, y, a in result:
if a == COL:
if y != 0 and (x, y - 1, COL) not in result and (x - 1, y, ROW) not in result and (x, y, ROW) not in result:
return False
else:
if (x, y - 1, COL) not in result and (x + 1, y - 1, COL) not in result and not (
(x - 1, y, ROW) in result and (x + 1, y, ROW) in result):
return False
return True
def solution(n, build_frame):
answer = []
for x, y, a, build in build_frame:
item = (x, y, a)
if build:
answer.append(item)
if not possible(answer):
answer.remove(item)
elif item in answer:
answer.remove(item)
if not possible(answer):
answer.append(item)
answer = [list(x) for x in sorted(answer, key=lambda item: (item[0], item[1], item[2]))]
return sorted(answer)