p_지게차와 크레인_388353

topics 600-알고리즘 & 코딩테스트
types 문제풀이

📚 613 BFS & DFS

문제

https://school.programmers.co.kr/learn/courses/30/lessons/388353
물류창고에 짐을빼냄
다빼는경우가잇고
겉에꺼만빼내는경우가 잇음

  1. 겉에꺼 확인시 변경중는 변경사항이 반영되면안됨
    1. deepcopy를 이용하여 복제
      에러 사항 빈공간이 안에 뚤려잇을때 (도넛형태)
      이를계속 돌아 메모리초과 에러가낫었음
      방문처리해주면해결됨
import copy

def solution(storage, requests):
    ylimit =len(storage)
    xlimit = len(storage[0])
    answer = ylimit * xlimit
    for i in range(ylimit):
        storage[i] = list(storage[i])
    # print(answer)
    for re in requests:
        if len(re) > 1:
            storage,num = takeAll(storage,re[0],ylimit,xlimit)
        else:
            storage,num = takeOutSide(storage,re[0],ylimit,xlimit)
            
        answer -= num
        # print(answer)
        # print(storage)
    
    return answer

def takeAll(storage,ch,ylim,xlim):
    cnt = 0 
    for i in range(ylim):
        for j in range(xlim):
            if storage[i][j] == ch:
                cnt += 1
                storage[i][j] = " "
    return [storage,cnt]


def takeOutSide(storage,ch,ylim,xlim):
    cnt = 0
    tstorage= []
    for i in range(ylim):
        tstor = copy.deepcopy(storage[i])
        for j in range(xlim):
            if tstor[j] == ch:
                tmp = checkIsOut(copy.deepcopy(storage),i,j,ylim,xlim)
                if tmp:
                    tstor[j] = " "
                    cnt += 1
        tstorage.append(tstor)
    storage = tstorage
    return [storage,cnt]
                
                
def checkIsOut(storage,y,x,ylim,xlim):
    move =[[0,1],[0,-1],[1,0],[-1,0]]
    if y == 0 or y == ylim-1 or x == 0 or x == xlim-1:
        return True
    for m in move:
        dy = y + m[0]
        dx = x + m[1]
        if storage[dy][dx] == " ":
            # print("")
            # 사이클될수도잇을듯
            storage[dy][dx] = "_"
            tmp = checkIsOut(storage,dy,dx,ylim,xlim)
            if tmp:
                return True
    return False