https://www.acmicpc.net/problem/17143
1. 코드
import copy
import sys
from collections import defaultdict
def reverse(a):
if a == 1:
return 2
elif a == 2:
return 1
elif a == 3:
return 4
elif a == 4:
return 3
def move(x, y, dir, speed):
global r, c
n_r = r - 1
n_c = c - 1
if dir == 1:
if x >= speed:
x -= speed
else:
k = speed - x
a, b = divmod(k, n_r)
if a % 2 == 0:
dir = reverse(dir)
x = b
else:
x = n_r - b
if x == 0:
dir = 1
elif x == n_r:
dir = 2
elif dir == 2:
if x + speed <= n_r:
x += speed
else:
k = speed - (n_r - x)
a, b = divmod(k, n_r)
if a % 2 == 0:
dir = reverse(dir)
x = n_r - b
else:
x = b
if x == 0:
dir = 1
elif x == n_r:
dir = 2
elif dir == 3:
if y + speed <= n_c:
y += speed
else:
k = speed - (n_c - y)
a, b = divmod(k, n_c)
if a % 2 == 0:
dir = reverse(dir)
y = n_c - b
else:
y = b
if y == 0:
dir = 4
elif y == n_c:
dir = 3
elif dir == 4:
if y >= speed:
y -= speed
else:
k = speed - y
a, b = divmod(k, n_c)
if a % 2 == 0:
dir = reverse(dir)
y = b
else:
y = n_c - b
if y == 0:
dir = 4
elif y == n_c:
dir = 3
return x, y, dir
ans = 0
r, c, m = map(int, (sys.stdin.readline().rstrip().split()))
per_col_shark = defaultdict(list)
for _ in range(m):
x, y, s, d, z = map(int, (sys.stdin.readline().rstrip().split()))
per_col_shark[y - 1].append([x - 1, s, d, z])
for i in range(c):
graph = [[0] * 100 for _ in range(100)]
temp = defaultdict(list)
if per_col_shark[i]:
per_col_shark[i].sort()
ans += per_col_shark[i].pop(0)[3]
for y in per_col_shark:
while per_col_shark[y]:
x, s, d, z = per_col_shark[y].pop()
nx, ny, nd = move(x, y, d, s)
graph[nx][ny] += 1
temp[ny].append([nx, s, nd, z])
for a in range(100):
for b in range(100):
if graph[a][b] >= 2:
new = []
target = []
for f in temp[b]:
if f[0] == a:
target.append(f)
else:
new.append(f)
new.append(sorted(target, key=lambda q: -q[3])[0])
temp[b] = new[:]
per_col_shark = copy.deepcopy(temp)
print(ans)