[SWEA] 1206.[S/W 문제해결 기본] 1일차 - View

야금야금 공부·2023년 4월 28일
0

SWEA

목록 보기
3/43
post-thumbnail

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV134DPqAA8CFAYh&categoryId=AV134DPqAA8CFAYh&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=PYTHON&select-1=3&pageSize=10&pageIndex=1


문제 풀이

  • 빌딩을 가로로 눕혔다고 생각한다.
    즉, n개의 빌딩 수를 행의 개수로 생각하고 길이만큼 1을 채운다.

  • 빌딩의 최대 길이는 255이므로 배열의 길이를 255까지 지정해준다.

  • arr[i][j] == 1이고, 배열의 양옆인 i-2, i-1, i+1, i+2를 비교해 모두 0이면 횟수 cnt에 더해준다.

import sys
sys.stdin = open("input.txt", "r")

for _ in range(1, 11):

    n = int(input())
    buildings = list(map(int, input().split()))
    arr = [[0] * (255) for _ in range(n)]

    for i in range(n):
        h = buildings[i]
        for j in range(h):  # 건물 높이만큼 1을 채운다
            arr[i][j] = 1

    cnt = 0
    for i in range(n):
        for j in range(255):
            if arr[i][j] == 1:  # 만약 1이라면, 양옆의 2칸이 비었는 지 확인
                if arr[i+1][j] == 0 and arr[i+2][j] == 0 and arr[i-1][j] == 0 and arr[i-2][j] == 0:
                    cnt += 1

    print(cnt)

0개의 댓글