PS
백준20056 ) 마법사 상어와 파이어볼🧨
문석2
2022. 6. 3. 10:57
반응형
삼성 코테 타입의 문제다. 그 중 치사하지 않은 편.
좌표 배열 만들어 할라했는데 N이 50이면 매 번 2500회 돌아야하니까 패스.
파이어볼의 위치를 dict로 만들어 구현했다.
dict로 구현해도 괜찮은 이유는 파이어볼이 움직일 때 제약사항이 하나도 없다. 범위를 벗어나는 것은 나머지 연산으로 쉽게 해결 가능.
dict를 하나만 쓰면 움직인 후 위치가 기존 위치와 겹칠 수 있어 move 함수 내에서 post_dic을 선언하고 리턴하는 방향으로 구현함
n,m,k = map(int,input().split())
fb = [list(map(int,input().split())) for i in range(m)]
diry = [-1,-1,0,1,1,1,0,-1]
dirx = [0,1,1,1,0,-1,-1,-1]
fb_dict = {}
for f in fb:
r,c,m,s,d = f[0],f[1],f[2],f[3],f[4]
fb_dict[(r,c)] = [[m,s,d]]
def move(prev):
post = {}
for key in prev.keys():
balls = prev[key]
y = key[0]
x = key[1]
for ball in balls:
m,s,d = ball[0],ball[1],ball[2]
ty = ((y+(diry[d]*s))+(10000*n))%n
tx = ((x+(dirx[d]*s))+(10000*n))%n
post_key = (ty,tx)
if post_key in post.keys():
post[post_key].append(ball)
else:
post[post_key] = [ball]
to_del = []
for key in post.keys():
balls = post[key]
sum_ball = len(balls)
if sum_ball==1:
continue
m_sum = 0
s_sum = 0
od,ev = 0,0
for ball in balls:
m,s,d = ball[0],ball[1],ball[2]
m_sum += m
s_sum += s
if d%2==1:
od+=1
else:
ev+=1
if m_sum//5==0:
to_del.append(key)
else:
s = s_sum//sum_ball
m = m_sum//5
post[key] = []
if od==0 or ev==0:
dir_arr = [0,2,6,4]
else:
dir_arr = [1,3,5,7]
for i in range(4):
post[key].append([m,s,dir_arr[i]])
for d in to_del:
del post[d]
return post
for i in range(k):
fb_dict = move(fb_dict)
ans = 0
for key in fb_dict.keys():
for ball in fb_dict[key]:
ans+=ball[0]
print(ans)
반응형