Skip to content

(22 복습) 삼성 SW 역량 테스트 기출 문제 / 백준 / 20056번 : 마법사 상어와 파이어볼: 격자의 행과 열은 1번부터 N번까지 번호가 매겨져 있고, 1번 행은 N번과 연결되어 있고, 1번 열은 N번 열과 연결되어 있다. #143

@sallyy1

Description

@sallyy1

image

격자의 행과 열은 1번부터 N번까지 번호가 매겨져 있고, 1번 행은 N번과 연결되어 있고, 1번 열은 N번 열과 연결되어 있다.

nx = (nx + N) % N, ny = (ny + N) % N

nx, ny = r + (dx[d]*s), c + (dy[d]*s)
nx = (nx + n) % n ###
ny = (ny + n) % n ###
  • (자료구조를 잘 활용하면, a[i][j] 배열 사용하지 않고 문제에서 주어진 파이어볼의 정보 리스트만 가지고 풀 수 있음)
  • 이번에는 수행 후 파이어볼의 정보를 Dictionary에 저장해봄

dx = [-1,-1,0,1,1,1,0,-1]
dy = [0,1,1,1,0,-1,-1,-1]


# 입력
n, m, k = map(int, input().split())

before_fireballs = [list(map(int, input().split())) for _ in range(m)]
# [행, 열, 질량, 속력, 방향] + 파이어볼 idx
# [0,  1,  2,   3,   4] + 5


## 매 턴마다 -> 합쳐지는 파이어볼의 정보를 임시로 저장할 배열이 필요함..
## 질량이 0인 파이어볼은 소멸되어 없어진다 => 다음 턴에서는 게임수행 대상이 안 됨 !!!


for turn in range(k):
    result = dict()
    fireballs = []

    # 1 단계. 모든 파이어볼의 이동
    for idx in range(len(before_fireballs)):
        r, c, m, s, d = before_fireballs[idx]
        ####print(r, c)

        if m == 0: ## (예외 처리 : 이전 턴에서 소멸된 파이어볼은 pass)
            continue

        nx, ny = r + (dx[d]*s), c + (dy[d]*s)
        nx = (nx + n) % n ###
        ny = (ny + n) % n ###

        result.setdefault((nx, ny),  [])
        result[(nx, ny)].append([m, s, d, idx])
                                # 0, 1, 2, 3

    ###print(result)

    # 2 단계. (2개 이상의 파이어볼이 있는 칸에서는) 파이어볼의 합쳐짐 + 나눠짐 + 소멸
    for key, value in result.items():
        ###print(key)
        ###print(value)
        new_i, new_j = key
        fire_lt = value

        if len(fire_lt) == 1:
            mm, ss, dd, idx = fire_lt[0]
            fireballs.append([new_i, new_j, mm, ss, dd])
            ################# 0,  1,  2,  3,  4
            # fireballs[idx][0] = i
            # fireballs[idx][1] = j ### 이동한 위치 정보로 업데이트
            continue


        m_sum = 0
        s_sum = 0
        for elem in fire_lt:
            mm, ss, dd, idx = elem
            m_sum += mm
            s_sum += ss

        new_m = m_sum // 5
        new_s = s_sum // len(fire_lt)


        # (질량이 0인 파이어볼은 소멸되어 없어진다.)
        if new_m == 0:
            continue

        # 4개의 파이어볼로 흝어뜨리기
        ### 1) 모두 홀수 이거나 2) 모두 짝수이면
        if all( (elem[2]%2 == 1)  for elem in fire_lt) == True or all( (elem[2]%2 == 0) for elem in fire_lt) == True:
            for new_d in [0, 2, 4, 6]:
                fireballs.append([new_i, new_j, new_m, new_s, new_d])

        else:
            for new_d in [1, 3, 5, 7]:
                fireballs.append([new_i, new_j, new_m, new_s, new_d])



    before_fireballs = fireballs[:] # 배열 정보 업데이트



# 남아 있는 파이어볼의 질량의 합 계산
score = 0
for elem in fireballs:
    score += elem[2]

print(score)


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions