거리두기 확인하기 - python(programmers)

참치돌고래·2021년 8월 30일
0

알고리즘

목록 보기
15/36
post-custom-banner

https://programmers.co.kr/learn/courses/30/lessons/81302


from collections import deque
    
    
def checking(stack):
    result = []
    for i, s in enumerate(stack):
        x1,y1 =stack[i]
        for j in range(i+1,len(stack)):
            x2,y2 = stack[j]
            if abs(x1-x2) + abs(y1-y2) <=2:
                result.append([x1,y1])
                result.append([x2,y2])
    return result



def bfs(stack,place):
    result = 0
    
    dx = [-1,1,0,0]
    dy = [0,0,-1,1]
    
    for s in range(0,len(stack),2):
        queue= deque()
        visited = [[0 for i in range(5)] for j in range(5)]
        
        start_x , start_y = stack[s]
        end_x, end_y = stack[s+1]
        x_length=abs(end_x - start_x)
        y_length = abs(end_y-start_y)
        
        queue.append([start_x,start_y])
        visited[start_x][start_y] = 1
        
        while queue:
            x,y=queue.popleft()
            print("x,y",x,y)
            if (x == end_x) & (y == end_y):
                print("find!!!!")
                return 0
            
            for i in range(4):
                next_x = x + dx[i]
                next_y = y + dy[i]
                print("next",next_x,next_y,i)
                print("min", min(start_x,end_x), min(start_y,end_y))
                print(x_length + min(end_x,start_x), y_length + min(end_y,start_y))
                if (min(start_x,end_x)<=next_x<=(x_length + min(end_x,start_x))) & (min(end_y,start_y)<=next_y<(y_length+min(start_y,end_y)+1)):
                    
                    if (place[next_x][next_y] != 'X') & (visited[next_x][next_y] == 0):
                       
                        queue.append([next_x,next_y])
                        visited[next_x][next_y] = 1
        
    
    return 1
def solution(places):
    answer = []
    
    
    
    
    start_x = 0
    start_y = 0
    for place_idx, place in enumerate(places):
        stack = []
        
        for x in range(len(place)):
            for y in range(len(place[0])):
                if place[x][y] == 'P':
                    stack.append([x,y])
        
        
        checkings = []
        
        checkings =checking(stack)
        
        
        
        
        if checkings == []:
            answer.append(1)
        
        else:
            answer.append(bfs(checkings,place))
        
             
            
    return answer
profile
안녕하세요
post-custom-banner

0개의 댓글