상세 컨텐츠

본문 제목

SWEA [모의 SW 역량테스트] 벽돌 깨기

Python/문제풀이 (삼성 A형 대비)

by 코딩하는 낙타 2020. 2. 18. 18:23

본문

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRQm6qfL0DFAUo&categoryId=AWXRQm6qfL0DFAUo&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

내 풀이:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import itertools
 
def go(j):
    for i in range(H):
        if new[i][j] != 0:
            bomb(i, j)
            break
 
 
def bomb(y, x):
    global H, W
    val = new[y][x]
    new[y][x] = 0
 
    for len in range(1, val):
        for dir in range(4):
            ny = y + len * dy[dir]
            nx = x + len * dx[dir]
 
            if 0 <= ny <= H-1 and 0 <= nx <= W-1 and new[ny][nx] != 0:
                bomb(ny, nx)
 
 
def gravity():
    for j in range(W):
        i = H-1
        while new[i][j] != 0 and i >= 0:
            i -= 1
 
        save = i
        i -= 1
 
        while i >= 0:
            if new[i][j] != 0:
                new[save][j] = new[i][j]
                new[i][j] = 0
                save -= 1
            i -= 1
 
dy = [010-1]
dx = [10-10]
 
= int(input())
for case in range(1, T+1):
    N, W, H = map(int,input().split())
    arr = [list(map(int,input().split())) for _ in range(H)]
    lst = list(itertools.product([i for i in range(W)], repeat = N))
    ans = 180
 
    for a in lst:
        new = [arr[_][:] for _ in range(H)]
 
        for b in a:
            go(b)
            gravity()
 
        res = 0
        for i in range(H):
            for j in range(W):
                if new[i][j] != 0:
                    res += 1
 
        ans = min(ans, res)
 
        if ans == 0:
            break
 
    print("#%d"%(case), ans)
 

Python3, 1078ms

코딩할 때 module 사용을 지양해왔는데 이번 문제를 풀 때는 itertools의 product를 사용해보았다. 문제를 풀 때 구슬을 떨어뜨릴 열을 선택하는 부분은 모두 탐색해주어야 한다고 판단하여 product로 리스트를 모두 뽑아 함수를 돌렸다. 베타 처리 방식을 모르는 상태에서 이 문제를 처음 접했을 때 문제 접근 방향을 찾기 어려워했으며 코딩도 엄청 지저분하게 작성되어 마음에 안 들었는데 이번에 문제를 생각보다 깔끔하게 풀어서 나름 만족한 문제이다.

관련글 더보기

댓글 영역