https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV597vbqAH0DFAVl&
def calc(dict):
total = 0
for rc, val in dict.items():
total += val[NUM]
return total
def solve():
global virus
for _ in range(M): # M 시간 뒤의 결과를 구해야 함
new = {} # 이동을 마친 바이러스를 넣을 딕셔너리
# 이동
for rc, val in virus.items():
num, direct, _ = val
next_row = rc[0] + dx[direct-1]
next_col = rc[1] + dy[direct-1]
if next_row == 0 or next_row == N-1 or next_col == 0 or next_col == N-1:
if direct == 1:
direct = 2
elif direct == 2:
direct = 1
elif direct == 3:
direct = 4
elif direct == 4:
direct = 3
num = num // 2
if num == 0:
continue
if (next_row, next_col) not in new:
new[(next_row, next_col)] = [num, direct, num]
else:
if num > new[(next_row, next_col)][MAX]: # 맥스 num보다 크면 direct를 교체
new[(next_row, next_col)][DIRECT] = direct
new[(next_row, next_col)][MAX] = num # max 교체
new[(next_row, next_col)][NUM] += num
virus = new
return calc(virus)
NUM, DIRECT, MAX = 0, 1, 2
T = int(input())
dx = (-1, 1, 0, 0)
dy = (0, 0, -1, 1)
for i in range(T):
N, M, K = map(int, input().split()) # 격자의 크기 N, 시간 M, 군집의 갯수 K
virus = {}
for _ in range(K):
x, y, n, d = map(int, input().split()) # 가로, 세로, 군집크기, 방향
virus[(x, y)] = [n, d, n] # 군집 크기, 방향, max(같은 셀에 모인 군집 중 가장 큰 크기. 현재는 자기 뿐이므로 자기 자신의 크기 n)
print("#{} {}".format(i+1, solve()))
'알고리즘 > 시뮬레이션' 카테고리의 다른 글
17822번. 원판 돌리기 (0) | 2020.05.02 |
---|---|
17140번. 이차원 배열과 연산 (0) | 2020.04.27 |
17144번. 미세먼지 안녕! (0) | 2020.04.21 |
16235번. 나무 재테크 (0) | 2020.04.20 |
15685번. 드래곤 커브 (0) | 2020.04.16 |