위클리 챌린지 3주차(2021.08) - pyhthon(programmers)

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

알고리즘

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

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


from collections import deque

def bfs(i,j,game_board):
    q = deque()
    
    dx = [1,-1,0,0]
    dy = [0,0,-1,1]
    q.append([i,j])
    visited =[]
    while q:
        x,y=q.popleft()
        visited.append([x,y])
        for i in range(4):
            next_x = dx[i] + x
            next_y = dy[i] + y
            
            if (0<=next_x<len(game_board)) and (0<=next_y<len(game_board[0])):
                
                if game_board[next_x][next_y] == 0:
                    if [next_x,next_y] not in visited:
                        
                        game_board[next_x][next_y] =1
                        q.append([next_x,next_y])
                    
    return visited
def bfs_puzzle(i,j,table):
    q = deque()
    
    dx = [1,-1,0,0]
    dy = [0,0,-1,1]
    q.append([i,j])
    visit =[]
    while q:
        x,y=q.popleft()
        visit.append([x,y])
        for i in range(4):
            next_x = dx[i] + x
            next_y = dy[i] + y
            
            if (0<=next_x<len(table)) and (0<=next_y<len(table[0])):
                if table[next_x][next_y] == 1:
                    if [next_x,next_y] not in visit:
                        
                        table[next_x][next_y] = 0
                        q.append([next_x,next_y])            
    return visit

        
def rotating(temps):
    rotate = []
    length = len(temps[0])
    
    tmps1=[[0 for i in range(length)] for j in range(length)]
    tmps2=[[0 for i in range(length)] for j in range(length)]
    tmps3=[[0 for i in range(length)] for j in range(length)]
    
    
    rotate.append(padding_0(temps))
    
    # 90 degree
    for i,temp in enumerate(temps):
        
        for j, tmp in enumerate(temp):
            tmps1[j][i] = temp[length-j-1]
    
    
    
     
    tmps1=padding_0(tmps1)
    rotate.append(tmps1)
    
    
    # 180 degree
    for i,temp in enumerate(temps):
        
        for j, tmp in enumerate(temp):
            tmps2[length-1-i][length-1-j] = temp[j]
            
    tmps2=padding_0(tmps2)
    
    rotate.append(tmps2)
    
    
    # 270 degree
    for i,temp in enumerate(temps):
        
        for j, tmp in enumerate(temp):
            tmps3[j][length-1-i] = temp[j]
            
    tmps3=padding_0(tmps3)
    rotate.append(tmps3)
    
    return rotate

def padding_0(tmps):
    temp = [[0 for i in range(len(tmps[0]))] for j in range(len(tmps[0]))]
    min_x = 50 
    min_y = 50
    for i,tmp in enumerate(tmps):
        for j,t in enumerate(tmp):
            if t ==1:
                min_x = min(min_x,i)
                min_y = min(min_y,j)
    
    for i, tmp in enumerate(tmps):
        for j,t in enumerate(tmp):
            if t ==1:
                temp[i-min_x][j-min_y] = t
            
    return temp
    
def padding(t):
    min_x = 50
    min_y = 50
    max_x = 0
    max_y= 0
    for x,y in t:
        min_x=min(x,min_x)
        min_y=min(y,min_y)
        
    for i in range(len(t)):
        t[i][0] -= min_x
        t[i][1] -= min_y
    for x,y in t:
        max_x = max(x,max_x)
        max_y = max(y,max_y)
        
    
    x = max(max_x,max_y)+1
        
    temp=[[0 for i in range(x)] for j in range(x)]
    
    for x,y in t:
        temp[x][y] =1
        
    
    return temp

    
def solution(game_board, table):
    answer = 0
    board = []
    puzzle=[]
    for i in range(len(game_board)):
        for j in range(len(game_board[0])):
            if game_board[i][j] == 0:
                board.append(bfs(i,j,game_board))
                
    
    for i in range(len(table)):
        for j in range(len(table[0])):
            
            if table[i][j] == 1:
                puzzle.append(bfs_puzzle(i,j,table))
                
    
    for i,tm in enumerate(board):
        board[i]=padding(tm)
        
    for i,p in enumerate(puzzle):
        puzzle[i] = padding(p)
    
    possible = []
    
    asd = [[0,0,1],[0,1,1],[0,1,0]]
    
    
    for b in board:
        
        i=0
        while i < len(puzzle):
            
            possible = rotating(puzzle[i])
            if b in possible:
                
                for p in possible[0]:
                    
                    answer +=p.count(1)
                del puzzle[i]
                break 
            else:
                i+=1
    
    
        
    return answer
profile
안녕하세요
post-custom-banner

0개의 댓글