파이썬으로 부시자! Python with Coreenee

프로그래머스 - 기둥과 보설치 - 구현

문제

스크린샷 2020-09-09 오전 12 15 10 스크린샷 2020-09-09 오전 12 15 35 스크린샷 2020-09-09 오전 12 15 45 스크린샷 2020-09-09 오전 12 16 01 스크린샷 2020-09-09 오전 12 16 20

입력

스크린샷 2020-09-09 오전 12 16 30


나의 처음 코드

각 조건들을 모듈화 하여, 구조물들을 삭제할때나 생성할 때 조건을 확인하여 수행해주면 된다. 구조물들의 생성규칙을 잘 파악하여, 각 좌표들을 저장을 해주는게 핵심 자료구조이다.


풀이 사진

개발-132

from collections import deque


def check_stick(pos, stick_list, bo_list):
    x, y = pos
    if y == 0:
        # 지면일 때
        return True
    if (x, y - 1) in stick_list:
        # 어떤 기둥위일 떄
        return True
    if (x - 1, y) in bo_list or (x, y) in bo_list:
        # 어떤 보 위일 때
        return True
    return False


def check_bo(pos, stick_list, bo_list):
    x, y = pos
    if (x, y - 1) in stick_list:
        # 보 시작좌표에 기둥이 있을 때
        return True
    if (x + 1, y - 1) in stick_list:
        # 보 끝좌표에 기둥이 있을 때
        return True
    if (x - 1, y) in bo_list and (x + 1, y) in bo_list:
        # 보 양쪽 끝에 보가 있을 떄
        return True
    return False


# 기둥 = 바닥위, 보의 한쪽 끝, 다른 기둥 위
# 보 = 한쪽 끝 부분이 기둥위, 양쪽 끝 부분이 다른 보
def solution(n, build_frame):
    answer = []
    stick_list = []
    bo_list = []
    for x, y, a, b in build_frame:
        # 구조물 삭제
        if b == 0:
            if a == 0:  # 기둥
                sflag = 0
                bflag = 0
                idx = stick_list.index((x, y))
                stick_list.pop(idx)
                for item in stick_list:
                    if not check_stick(item, stick_list, bo_list):
                        sflag = 1
                        break
                for item in bo_list:
                    if not check_bo(item, stick_list, bo_list):
                        bflag = 1
                        break
                if sflag or bflag:
                    stick_list.append((x, y))
            else:  # 보
                sflag = 0
                bflag = 0
                idx = bo_list.index((x, y))
                bo_list.pop(idx)
                for item in stick_list:
                    if not check_stick(item, stick_list, bo_list):
                        sflag = 1
                        break
                for item in bo_list:
                    if not check_bo(item, stick_list, bo_list):
                        bflag = 1
                        break
                if sflag or bflag:
                    bo_list.append((x, y))

        else:  # 구조물 생성
            if a == 0:  # 기둥
                if check_stick((x, y), stick_list, bo_list):
                    stick_list.append((x, y))
            else:  # 보
                if check_bo((x, y), stick_list, bo_list):
                    bo_list.append((x, y))

    for x, y in stick_list:
        answer.append((x, y, 0))
    for x, y in bo_list:
        answer.append((x, y, 1))

    answer.sort(key=lambda x: (x[0], x[1], x[2]))
    return answer

좌표를 어떻게 저장하느냐와 조건을 잘 설계했느냐가 이 문제의 핵심이다.