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()) fireball = {} for _ in range(m): temp = list(map(int,input().split())) fireball[(temp[0]-1,temp[1]-1)] = [] # 질량, 속도, 방향 fireball[(temp[0]-1,temp[1]-1)].append([temp[2],temp[3],temp[4]]) for _ in range(k): new_fireball = {} for ind,vals in fireball.items(): for val in vals: new_x = (ind[0] + val[1]*dx[val[2]])%n new_y = (ind[1] + val[1]*dy[val[2]])%n if new_fireball.get((new_x,new_y)): new_fireball[(new_x,new_y)].append([val[0],val[1],val[2]]) else: new_fireball[(new_x,new_y)] = [[val[0],val[1],val[2]]] fireball ={} for ind,vals in new_fireball.items(): if len(vals) > 1: total_weight = 0 total_speed = 0 total_direction = [] for val in vals: total_weight += val[0] total_speed += val[1] total_direction.append(val[2]) next_weight = total_weight//5 next_speed = total_speed//len(vals) if next_weight: total_direction = list(map(lambda x: x%2 ,total_direction)) if sum(total_direction) == 0 or sum(total_direction) == len(vals): next_direction = [0,2,4,6] else: next_direction = [1,3,5,7] fireball[ind] = [] for i in range(4): fireball[ind].append([next_weight,next_speed,next_direction[i]]) else: fireball[ind] = vals result = 0 for ind,vals in fireball.items(): for val in vals: result += val[0] print(result)
문제에 나온 내용을 그대로 시뮬레이션을 해주면 되는 것이었다.
이러한 무한의 길이를 가진 행렬문제나, 한 위치에 여러가지 정보를 저장해야하는 경우가
나오면 리스트로 구현하기 보단, 딕셔너리로 해서 위치좌표를 키로 지정하고 밸류 값에 여러가지 정보를 저장하는 방식을 선호한다.
이 문제에서 주의해야할 점은 행렬의 범위를 벗어나면 반대편으로 나오는 것이다.
이 부분은 나는 n으로 나눈 나머지로 해결했다. 그 뒤에는 딕셔너리에 저장된 값 중 길이가 2 이상인 경우에는 문제에서 주어졌던 조건에 맞추어, 분리가 되도록 구현했다.
'알고리즘 > 백준' 카테고리의 다른 글
[BOJ] 18352 특정 거리의 도시 찾기 (0) | 2021.01.10 |
---|---|
[BOJ] 20055 컨베이어 벨트 위의 로봇 (0) | 2021.01.10 |
[BOJ] 20057 마법사 상어와 토네이도 (0) | 2021.01.10 |
[BOJ] 7562 나이트의 이동 (0) | 2021.01.10 |
[BOJ] 16918 봄버맨(풀이 방식 2종류) (0) | 2021.01.10 |