2021 카카오 채용연계형 인턴쉽 - 거리두기 확인하기 (Level 2)

 

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


Code

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
from itertools import product, combinations

dist = lambda p1, p2: abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

def solution_one(place):
    idxs_P = [(i, j) for i, j in product(range(len(place)), range(len(place[0]))) if place[i][j] == 'P']
    idxs_O = [(i, j) for i, j in product(range(len(place)), range(len(place[0]))) if place[i][j] == 'O']
    idxs_X = [(i, j) for i, j in product(range(len(place)), range(len(place[0]))) if place[i][j] == 'X']

    ps = list(combinations(idxs_P, 2))
    ds = []
    for p1, p2 in ps:
        if (d := dist(p1, p2)) == 1:
            return 0
        ds.append(d)
    else:
        for (p1, p2), d in zip(ps, ds):
            if d == 2:
                m = ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)
                if (m[0] % 1) == 0:  # P-P
                    if (int(m[0]), int(m[1])) in idxs_O:
                        return 0
                else:  # P ㄱ P
                    upper_left = (min(p1[0], p2[0]), min(p1[1], p2[1]))
                    if upper_left in (p1, p2):
                        if not (((upper_left[0] + 1, upper_left[1]) in idxs_X) and (
                                (upper_left[0], upper_left[1] + 1) in idxs_X)):
                            return 0
                    else:
                        if not (((upper_left[0], upper_left[1]) in idxs_X) and (
                                (upper_left[0] + 1, upper_left[1] + 1) in idxs_X)):
                            return 0
    return 1
def solution(places):
    return [solution_one(place) for place in places]

Complexity

$O(1)$