[PS] 거리두기 확인하기

owo·2023년 1월 24일
0

PS

목록 보기
5/25

문제 링크

코드

def solution(places):
    return  list(map(lambda x: 1 if validate(x) else 0, places))


def validate(place):
    for y, row in enumerate(place):
        for x, cell in enumerate(row):
            if cell == "P":
                if not (validate_case1(place, y, x) and validate_case2(place, y, x) and validate_case3(place, y, x)):
                    return False
    return True


def validate_case1(place, y, x):
    case1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    
    for dy, dx in case1:
        (ny, nx) = (y + dy, x + dx)
        if ny in range(0, 5) and nx in range(0, 5) and place[ny][nx] == "P":
            return False
        
    return True
        
    
def validate_case2(place, y, x):
    case2 = [(-2, 0), (2, 0), (0, -2), (0, 2)]
    
    for dy, dx in case2:
        (ny, nx) = (y + dy, x + dx)
        if ny in range(0, 5) and nx in range(0, 5) and place[ny][nx] == "P":
            (py, px) = (y + dy // 2, x + dx // 2)
            if place[py][px] != "X":
                return False
            
    return True
        
    
def validate_case3(place, y, x):
    case3 = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
    
    for dy, dx in case3:
        (ny, nx) = (y + dy, x + dx)
        if ny in range(0, 5) and nx in range(0, 5) and place[ny][nx] == "P":
            (py1, px1) = (y + dy, x)
            (py2, px2) = (y, x + dx)
            if place[py1][px1] != "X" or place[py2][px2] != "X":
                return False
            
    return True

리뷰

문제 예시에서 케이스 분석까지 되어있어서 차분하게 구현하면 풀 수 있는 문제였다

0개의 댓글