프로그래머스/LEVEL 2

거리두기 확인하기

GenieLove! 2022. 2. 17. 10:39
728x90
반응형

Python

a, b = [-1, 0, 0, 1], [0, -1, 1, 0]#이중배열은 상하좌우 좌표로 푸는 거 기억!
#a, b 리스트는 상하좌우 하나씩 움직일 수 있는 배열!
#상하좌우 검사 후 p가 있으면 0, 상하좌우가 O로 되어있고, O의 상하좌우에 P가 0(자기자신 빼고)
def solution(places):
    answer = []
    
    for place in places:
        answer.append(checkIsOk(place))
                        
    return answer

def checkIsOk(places):
    for i, place in enumerate(places):
        for j in range(len(place)):
            if place[j] != "P":
                continue
            for k in range(4):
                if (i+a[k]) >= 0 and (j+b[k]) >= 0 and (i+a[k]) < 5 and (j+b[k]) < 5:
                    if places[i+a[k]][j+b[k]] == "P":
                        return 0
                    if places[i+a[k]][j+b[k]] == "O":
                        if checkMore(places, i+a[k], j+b[k], i, j):
                            return 0
    return 1

def checkMore(places, x, y, no_check_x, no_check_y):
    for k in range(4):
        if x+a[k] >= 0 and y+b[k] >= 0 and x+a[k] < 5 and y+b[k] < 5:
            if x+a[k] == no_check_x and y+b[k] == no_check_y:
                continue
            if places[x+a[k]][y+b[k]] == "P":
                return True
    return False
728x90
반응형

'프로그래머스 > LEVEL 2' 카테고리의 다른 글

피로도  (0) 2022.04.12
후보키  (0) 2022.02.19
타겟 넘버  (0) 2021.12.19
수식 최대화  (0) 2021.06.06
튜플  (0) 2021.06.03