[백준] 20056번: 마법사 상어와 파이어볼

yoonene·2022년 10월 13일
0

알고리즘

목록 보기
19/62

문제 이동

이해 못했던 문장
"격자의 행과 열은 1번부터 N번까지 번호가 매겨져 있고, 1번 행은 N번과 연결되어 있고, 1번 열은 N번 열과 연결되어 있다."

코드는 단순 구현이라 쉬웠지만 위 문장의 의미가 원형 큐처럼 연속되는 격자라는 것을 이해하지 못해서 오래 걸렸다.
삼성 문제는 항상 문제를 이해하기 어렵게 어려운 말로 표현하는 것 같다.


N, M, K = map(int, input().split())
fireballs = []
for _ in range(M):
    r, c, m, s, d = map(int, input().split())
    fireballs.append([r-1, c-1, m, s, d])

dir = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

for _ in range(K):
    map = [[[] for _ in range(N)] for _ in range(N)]
    # 이동
    for ball in fireballs:
        s, d = ball[-2], dir[ball[-1]]
        cr, cc = ball[0], ball[1]
        nr, nc = (cr + d[0] * s) % N, (cc + d[1] * s) % N
        map[nr][nc].append(ball[2:])
    fireballs = []
    for row in range(N):
        for col in range(N):
            # 2개 이상의 파이어볼
            cnt = len(map[row][col])
            if cnt > 1:
                sum_m, sum_s = 0, 0
                even, odd = 0, 0
                for i in range(cnt):
                    ball = map[row][col][i]
                    sum_m += ball[0]
                    sum_s += ball[1]
                    if ball[2] % 2 == 0:
                        even += 1
                    else:
                        odd += 1
                nm = sum_m // 5
                ns = sum_s // cnt
                if nm != 0:
                    if even == cnt or odd == cnt:
                        for d in [0, 2, 4, 6]:
                            fireballs.append([row, col] + [nm, ns, d])
                    else:
                        for d in [1, 3, 5, 7]:
                            fireballs.append([row, col] + [nm, ns, d])

            elif cnt == 1:
                fireballs.append([row, col] + map[row][col][0])


answer = sum(ball[2] for ball in fireballs)
print(answer)
profile
NLP Researcher / Information Retrieval / Search

0개의 댓글