목차

거리두기 확인하기

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호81302
문제명거리두기 확인하기
레벨Level 2
분류

구현

시간복잡도O(r*c)
인풋사이즈r=5, c=5
사용한 언어Python
해결날짜2021/07/09
출처

ps:problems:programmers:2021_카카오_채용연계형_인턴십

풀이

코드

"""Solution code for "Programmers 81302. 거리두기 확인하기".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/81302
- Solution link: http://www.teferi.net/ps/problems/programmers/81302
"""

DELTA = ((1, 0), (0, 1), (-1, 0), (0, -1))


def is_okay(place):
    def is_person_at_pos(r, c):
        return 0 <= r < 5 and 0 <= c < 5 and place[r][c] == 'P'
    
    for r in range(5):
        for c in range(5):
            if not is_person_at_pos(r, c):
                continue
            for (dr1, dc1), (dr2, dc2) in zip(DELTA, DELTA[1:] + DELTA[:1]):
                nr, nc = r + dr1, c + dc1
                nnr, nnc = r + dr1 + dr1, c + dc1 + dc1
                nr2, nc2 = r + dr2, c + dc2
                diag_r, diag_c = r + dr1 + dr2, c + dc1 + dc2
                if is_person_at_pos(nr, nc):
                    return False
                if is_person_at_pos(nnr, nnc) and place[nr][nc] != 'X':
                    return False
                if (is_person_at_pos(diag_r, diag_c) and 
                        (place[nr][nc] != 'X' or place[nr2][nc2] != 'X')):
                    return False            
    return True


def solution(places):
    return [1 if is_okay(x) else 0 for x in places]