url : https://programmers.co.kr/learn/courses/30/lessons/17679
사진이 많으므로 URL 참조 부탁드립니다 : )
사진이 많으므로 URL 참조 부탁드립니다 : )
- 프로그래머스에서 LEVEL 2로 되어있다.
- 단순한 구현이다 아래의 순서에 맞게만 코드를 진행해주면 된다.
1 )겹쳐진 4개의 블록이 있는지 확인
블록 확인 시 주의 할 점은 겹쳐서 터지는 블록도 있다는 것!
2 ) 겹쳐진 블록을 터트리며 터진 블록 수 세기
겹쳐서 터지는 블록은 모두 1개로 취급되어야 함 만일 4의 배수로 답이 나온다면 겹치는 블록을 복수로 처리했는지 확인 해주세요
3 ) 문제의 조건처럼 블록 재정렬
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문으로 처리면 되는데 뭔가 귀찮다고 구현 안했더니 코드가 더러워졌다....
다음부턴 그러지말자..