본문 바로가기
알고리즘/소스코드

PROGRAMMERS 17679: [1차] 프렌즈4블록

by cjw.git 2021. 1. 13.

Link : programmers.co.kr/learn/courses/30/lessons/17679


Python

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def solution(m, n, board):
    ## 90도 회전 ##
    board = [[board[i][j] for i in range(m)] for j in range(n)]
    for i in range(n):
        board[i].reverse()
    ##############
    y, x = m, n
    total = 0
    while True:
        stack = []
        for i in range(x - 1):
            for j in range(y - 1):
                ## 4개의 요소가 전부 같은지 확인 ##
                data = board[i][j]
                if data == board[i][j + 1] \
                        == board[i + 1][j] \
                        == board[i + 1][j + 1] \
                        and data != 0:
                    ## 같으면 스택에 추가 ##
                    stack.append((i, j))  # y, x
                ################################
        ## 스택이 0보다 크면 제거 목록이 있으므로 처리해줌 ##
        if len(stack) > 0:
            for i, j in stack:
                ## 이미 지워진 것은 계산하지 않도록 작업 ##
                if board[i][j] != 0:
                    board[i][j] = 0
                    total += 1
                if board[i + 1][j] != 0:
                    board[i + 1][j] = 0
                    total += 1
                if board[i][j + 1!= 0:
                    board[i][j + 1= 0
                    total += 1
                if board[i + 1][j + 1!= 0:
                    board[i + 1][j + 1= 0
                    total += 1
                #####################################
            for i in range(n):
                counter = 0
                temp = []
                ## 0의 개수를 세고 0이 아니면 앞으로 밀어넣음 ##
                for j in range(m):
                    if board[i][j] == 0:
                        counter += 1
                    else:
                        temp.append(board[i][j])
                ## 0의 개수만큼 뒤에다 넣어줌 ##
                for t in range(counter):
                    temp.append(0)
                board[i] = temp
        else:
            break
    return total
cs

FeedBack

  1.  

 

 

 

cjw.git@gmail.com

댓글