프로그래머스 - 거리두기 확인하기

한음·2022년 3월 3일
0

문제 보기

def solution(places):
    answer = []
    for place in places:
        tmp = False
        for i in range(5):
            for j in range(5):
                if place[i][j] == "P":
                    memo = [[0 for i in range(5)] for _ in range(5)]
                    result = []
                    finder(place, (i, j), i, j, memo, 0, result)
                    if result:
                        tmp = True
                        answer.append(0)
                        break
            if tmp:
                break
        if not tmp:
            answer.append(1)
    return answer


def finder(graph, start, x, y, memo, count, result):
    if x < 0 or y < 0:
        return
    if (x, y) == start:  # 사방으로 보내기
        memo[x][y] = 1
        count += 1
        finder(graph, start, x, y - 1, memo, count, result)
        finder(graph, start, x, y + 1, memo, count, result)
        finder(graph, start, x + 1, y, memo, count, result)
        finder(graph, start, x - 1, y, memo, count, result)
        return
    if result or count > 2:
        # 결과가 나왔거나, 2칸 이상 이동했거나, 이미 방문한 점이면 종료
        return
    try:
        current = graph[x][y]
        visited = memo[x][y]
    except IndexError:
        return
    if visited:
        return
    memo[x][y] = 1

    if current == "P" and count <= 2:  # hit
        result.append(1)
        return
    elif current == "X":
        return
    else:  # 비어있는 경우 -> 사방으로 보낸다
        count += 1
        finder(graph, start, x, y - 1, memo, count, result)
        finder(graph, start, x, y + 1, memo, count, result)
        finder(graph, start, x + 1, y, memo, count, result)
        finder(graph, start, x - 1, y, memo, count, result)

memo 랑 result 이용해 최대한 중복 실행 줄이려 했다.

profile
https://github.com/0hhanum

0개의 댓글