-
거리두기 확인하기코딩 테스트/Level 2 2021. 7. 22. 07:45반응형
https://programmers.co.kr/learn/courses/30/lessons/81302
level 2 답게...
def solution(places): return [f(place) for place in places] def f(place): height, width = 5, 5 for h in range(height): for w in range(width): if place[h][w] != 'P': continue if h - 2 >= 0 and place[h - 2][w] == 'P' and place[h - 1][w] != 'X': return 0 if h + 2 < height and place[h + 2][w] == 'P' and place[h + 1][w] != 'X': return 0 if w - 2 >= 0 and place[h][w - 2] == 'P' and place[h][w - 1] != 'X': return 0 if w + 2 < width and place[h][w + 2] == 'P' and place[h][w + 1] != 'X': return 0 if h - 1 >= 0 and place[h - 1][w] == 'P': return 0 if h + 1 < height and place[h + 1][w] == 'P': return 0 if w - 1 >= 0 and place[h][w - 1] == 'P': return 0 if w + 1 < width and place[h][w + 1] == 'P': return 0 if h - 1 >= 0 and w - 1 >= 0 and place[h - 1][w - 1] == 'P': if place[h - 1][w] != 'X' or place[h][w - 1] != 'X': return 0 if h + 1 < height and w + 1 < width and place[h + 1][w + 1] == 'P': if place[h + 1][w] != 'X' or place[h][w + 1] != 'X': return 0 if h - 1 >= 0 and w + 1 < width and place[h - 1][w + 1] == 'P': if place[h - 1][w] != 'X' or place[h][w + 1] != 'X': return 0 if h + 1 < height and w - 1 >= 0 and place[h + 1][w - 1] == 'P': if place[h + 1][w] != 'X' or place[h][w - 1] != 'X': return 0 return 1
반응형