프로그래머스 거리두기

DARTZ·2022년 5월 27일
0

알고리즘

목록 보기
79/135

실패 코드..

def solution(places):
    direction_x = [1, -1, 0, 0]
    direction_y = [0, 0, 1, -1]
    answer = []
    
    def dfs(x, y, place, standard):
        visited[x][y] = True

        for d in range(4):
            next_x = direction_x[d] + x
            next_y = direction_y[d] + y

            if 0 <= next_x < 5 and 0 <= next_y < 5 and visited[next_x][next_y] == False:

                if place[next_x][next_y] == 'X':
                    break

                elif place[next_x][next_y] == 'P':
                    manhattan = abs(
                        next_x - standard[0]) + abs(next_y - standard[1])

                    if manhattan <= 2:
                        return 0

                    return 1

                else:
                    dfs(next_x, next_y, place, standard)
                    
    for place in places:
        not_able = 0

        for x in range(5):
            for y in range(5):
                if place[x][y] == 'P':
                    visited = [[False] * 5] * 5
                    standard = [x, y]
                    flag = dfs(x, y, place, standard)
                    if flag == 0:
                        not_able = 1
                        break

        if not_able == 1:
            answer.append(0)

        else:
            answer.append(1)
    
    
    return answer
profile
사람들이 비용을 지불하고 사용할 만큼 가치를 주는 서비스를 만들고 싶습니다.

0개의 댓글