본문 바로가기

PS/프로그래머스

[프로그래머스 LV.3] 기둥과 보 설치(파이썬/python)

728x90

https://programmers.co.kr/learn/courses/30/lessons/60061

 

코딩테스트 연습 - 기둥과 보 설치

5 [[1,0,0,1],[1,1,1,1],[2,1,0,1],[2,2,1,1],[5,0,0,1],[5,1,0,1],[4,2,1,1],[3,2,1,1]] [[1,0,0],[1,1,1],[2,1,0],[2,2,1],[3,2,1],[4,2,1],[5,0,0],[5,1,0]] 5 [[0,0,0,1],[2,0,0,1],[4,0,0,1],[0,1,1,1],[1,1,1,1],[2,1,1,1],[3,1,1,1],[2,0,0,0],[1,1,1,0],[2,2,0,1]] [[

programmers.co.kr

 

 

단순 구현문제였는데 오랜만에 알고리즘 풀어서 좀 헷갈렸다

 

주석으로 조건을 적어놨음

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def solution(n, build_frame):
    answer = []
 
    # build_frame 순회
    for build in build_frame:
        # 가로좌표,세로좌표,구조물,설치삭제여부
        x, y, a, b = build
 
        # 설치
        if b == 1:
            # 기둥일경우
            if a == 0:
                # 바닥에 있을경우 설치가능
                if y == 0:
                    answer.append([x, y, a])
 
                else:
                    if [x, y - 10in answer or [x - 1, y, 1in answer or [x, y, 1in answer:
                        answer.append([x, y, a])
 
            # 보일경우
            elif a == 1:
                if [x, y - 10in answer or [x + 1, y - 10in answer or (
                        [x - 1, y, 1in answer and [x + 1, y, 1in answer):
                    answer.append([x, y, a])
        # 제거
        elif b == 0:
            # 기둥일경우
            if a == 0:
                # 기둥이 제거됨으로서 위에있는 기둥이나 보가 설치 불가조건이 될 경우 제거못함
                # 위에 기둥이 있을경우
                if [x, y + 10in answer:
                    # 지탱해줄수 없으면 실패
                    if [x - 1, y + 11not in answer and [x, y + 11not in answer:
                        continue
 
                # 왼쪽 위에 보가 있을경우 왼쪽 기둥이 없거나 양쪽 보가 없으면 실패
                if [x - 1, y + 11in answer and ([x - 1, y, 0not in answer and (
                        [x - 2, y + 11not in answer or [x, y + 11not in answer)):
                    continue
                # 오른쪽 위에 보가있을경우 오른쪽 기둥이 없거나 양쪽 보가 없으면 실패
                if [x, y + 11in answer and ([x + 1, y, 0not in answer and (
                        [x - 1, y + 11not in answer or [x + 1, y + 11not in answer)):
                    continue
 
                answer.remove([x, y, a])
 
 
            # 보 일경우
            elif a == 1:
                # 사라짐으로 인해 위의 기둥이 유지할수 없거나 보의 설치조건이 깨지지 않는다면 제거가능
                # 왼쪽 위에 기둥이 있을경우
                if [x, y, 0in answer:
                    # 기둥을 지탱해줄 기둥이없거나 보가 없으면 실패
                    if [x, y - 10not in answer and [x - 1, y, 1not in answer:
                        continue
                # 오른쪽 위에 기둥이 있을경우
                if [x + 1, y, 0in answer:
                    # 기둥을 지탱해줄 기둥이없거나 보가 없으면 실패
                    if [x + 1, y - 10not in answer and [x + 1, y, 1not in answer:
                        continue
                # 왼쪽에 보가있을떄
                if [x - 1, y, 1in answer:
                    # 왼쪽보의 왼쪽과 오른쪽에 기둥이 없으면 실패
                    if [x - 1, y - 10not in answer and [x, y - 10not in answer:
                        continue
                # 오른쪽에 보가 있을때
                if [x + 1, y, 1in answer:
                    # 오른쪽보의 왼쪽과 오른쪽에 기둥이 없으면 실패
                    if [x + 1, y - 10not in answer and [x + 2, y - 10not in answer:
                        continue
 
                answer.remove([x, y, a])
 
    answer = sorted(answer, key=lambda x: (x[0], x[1], x[2]))
 
    return answer
 
cs
728x90