Lv2 - 거리두기 확인하기

LeeKyoungChang·2022년 4월 29일
0

Algorithm

목록 보기
186/203
post-thumbnail

📚 Lv2 - 거리두기 확인하기

거리두기 확인하기

 

이해

문제에서 이미 조건들을 다 제시해주었다.

✏️ 조건
(1) 맨해튼 거리가 2이하로 앉지 말아주세요.
(2) 단, 응시자가 앉아있는 자리 사이가 파티션으로 막혀 있을 경우에는 허용한다.

  • [프로그래머스 문제집 사진]

 

✔️ 내가 생각한 경우의 수
현재 P의 위치를 찾았을 때

  • bfs, dfs풀 때 상하좌우를 사용하듯이 상하좌우에 P가 존재한다면 False
  • 상하좌우 * 2 에서 P가 존재할 때, 상하좌우가 O라면 False
  • 만약, 대각선에 P가 존재한다면, 해당 상하좌우에(P가 오른쪽 위에 있다면, 위와 오른쪽) O가 있는지 확인하며, 있다면 False이다.

 

소스

# 상하좌우
x_coordinate = [-1, 0, 1, 0]
y_coordinate = [0, 1, 0, -1]

# 대각선
x_diagonal = [-1, -1, 1, 1]
y_diagonal = [-1, 1, 1, -1]


def check_place(place):
    arr = []
    for i in range(len(place)):
        arr.append(list(map(str, place[i].strip())))
    
    for i in range(5):
        for j in range(5):
            if arr[i][j] == 'P':
                # 상하좌우 확인한다.
                for rc in range(4):
                    next_x = x_coordinate[rc] + i
                    next_y = y_coordinate[rc] + j
                    
                    next_next_x = x_coordinate[rc] * 2 + i
                    next_next_y = y_coordinate[rc] * 2 + j
                    
                    
                    if 0<= next_x <5 and 0<=next_y <5:
                        if arr[next_x][next_y] == 'P':
                            return False
								# 만약 상하좌우에 O가 있다면, 한 칸 더 옆에 P가 있는지 확인한다.
                        if arr[next_x][next_y] == 'O' and 0 <= next_next_x <5 and 0<= next_next_y < 5:
                            if arr[next_next_x][next_next_y] == 'P':
                                return False
                # 대각선 상하좌우 확인한다.            
                for rc in range(4):
                    next_x = x_diagonal[rc] + i
                    next_y = y_diagonal[rc] + j
                    
                    if 0<= next_x <5 and 0<=next_y <5 and arr[next_x][next_y] == 'P':
                        if arr[next_x][j] == 'O':
                            return False
                        
                        if arr[i][next_y] == 'O':
                            return False
    return True

def solution(places):
    answer = []
    
    for in_place in places:
        answer.append(int(check_place(in_place)))
    
    return answer

 

profile
"야, (오류 만났어?) 너두 (해결) 할 수 있어"

0개의 댓글