Skip to content

Commit

Permalink
Programmers DFS
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrevile committed Jan 31, 2022
1 parent afb785b commit c79c2da
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Programmers/거리두기 확인하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 대기실은 5개이며, 각 대기실은 5x5 크기입니다.
# 거리두기를 위하여 응시자들 끼리는 맨해튼 거리1가 2 이하로 앉지 말아 주세요.
# 단 응시자가 앉아있는 자리 사이가 파티션으로 막혀 있을 경우에는 허용합니다
def solution(places):
answer = []
for place in places:
answer.append(find_illegal(place))
return answer


def find_illegal(place):
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]

find = []

def DFS(x, y, start_x, start_y):
visited[x][y] = True
answer = True
for dir in direction:
dx = x + dir[0]
dy = y + dir[1]
if 0 <= dx < 5 and 0 <= dy < 5:
if visited[dx][dy] == False and calculate_distance(start_x, start_y, dx, dy) <= 2:
if place[dx][dy] == "P":
return False
else:
answer = DFS(dx, dy, start_x, start_y)
if answer == False:
return False
return answer

for i in range(5):
for k in range(5):
if place[i][k] == "P":
visited = []
for row in range(5):
temp = []
for col in range(5):
if place[row][col] == "X":
temp.append(True)
else:
temp.append(False)
visited.append(temp)
temp = DFS(i, k, i, k)
if temp == False:
return 0
return 1


def calculate_distance(r1, c1, r2, c2):
return abs(r1 - r2) + abs(c1 - c2)

0 comments on commit c79c2da

Please sign in to comment.