프로그래머스 [1차] 프렌즈 4블록

최세찬·2021년 8월 10일
0

🙂 문제 - [1차] 프렌즈 4블록

url : https://programmers.co.kr/learn/courses/30/lessons/17679

✔️ 문제 내용

사진이 많으므로 URL 참조 부탁드립니다 : )

❌ 제한 사항

사진이 많으므로 URL 참조 부탁드립니다 : )

🖐풀이 방법

  • 프로그래머스에서 LEVEL 2로 되어있다.
  • 단순한 구현이다 아래의 순서에 맞게만 코드를 진행해주면 된다.
    1 )겹쳐진 4개의 블록이 있는지 확인
    블록 확인 시 주의 할 점은 겹쳐서 터지는 블록도 있다는 것!
    2 ) 겹쳐진 블록을 터트리며 터진 블록 수 세기
    겹쳐서 터지는 블록은 모두 1개로 취급되어야 함 만일 4의 배수로 답이 나온다면 겹치는 블록을 복수로 처리했는지 확인 해주세요
    3 ) 문제의 조건처럼 블록 재정렬

📃 CODE

   import copy

def change(check):
   xlen = len(check)
   ylen = len(check[0])
   
   for j in range(ylen):
       
       idx = xlen-1
       for i in range(xlen-1,-1,-1):
           
           if check[idx][j] == '0' and check[i][j]!='0':    
               check[idx][j] = check[i][j]
               check[i][j]='0'
               idx-=1
           elif check[idx][j] !='0':
               idx-=1
           
   
   return check
   
   
def cul(board,cnt):
   xlen = len(board)
   ylen = len(board[0])
   check = copy.deepcopy(board)
   
   for i in range(xlen-1):
       for j in range(ylen-1):
           if board[i][j] =='0':
                   continue;
           if board[i][j] == board[i+1][j] and board[i][j] == board[i+1][j+1] and board[i][j] == board[i][j+1]:
               
               if check[i][j] != '0':
                   cnt +=1
                   check[i][j] = '0'
               if check[i][j+1] != '0':
                   cnt +=1
                   check[i][j+1] = '0'
               if check[i+1][j] != '0':
                   cnt +=1
                   check[i+1][j] = '0'
               if check[i+1][j+1] != '0':
                   cnt +=1
                   check[i+1][j+1] = '0'
               
   board = change(check)
          
   return cnt,board
def tolist(get):
   result = []
   for string in get:
       mid = []
       for s in string:
           mid.append(s)
       result.append(mid)
   return result
def solution(m, n, board):
   answer = 0
   cnt = 0 
   becnt = -1
   board = tolist(board)
   
   while (becnt != cnt):
       becnt = cnt
       cnt ,board= cul(board,becnt)
       
       
   return cnt

cul 함수 그냥 for문으로 처리면 되는데 뭔가 귀찮다고 구현 안했더니 코드가 더러워졌다....
다음부턴 그러지말자..

profile
느리지만 계속해서 성장의 가치를 알고 있습니다.

0개의 댓글